Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsTree "create_node" really not working

Tags:

jquery

jstree

I am trying to dynamically add a new node in my jsTree container. It is not working at all. I don't know what i am missing.

jsfiddle example

$("#tree").jstree({
    core: {
        "animation": 0
    },
    "html_data": {},
    "themes": {
        "icons": false
    },
    "search": {
        "case_insensitive": true,
        "show_only_matches": true
    },
    "plugins": ["themes", "html_data", "search"]
});

$("#tree").jstree("create_node", $("node_1"), "after", { "data": "Hello World" });

html:

<div id="tree">
    <ul>
        <li id="node1"><a>Hello</a></li>
    </ul>
</div>
like image 918
Catalin Avatar asked Jul 04 '12 11:07

Catalin


4 Answers

set 'check_callback' : true, otherwise all operations like create, rename are prevented. like so:

        this.treeDiv.jstree(
                {

                    'core' : {
                        'check_callback': true,
                        'data' : {
                            'url' : function (node) {
                            return 'ajax_roots.json';
                            },
                            'data' : function (node) {}
                            }
                    },
                    "search" : {
                        "fuzzy" : false,
                        "show_only_matches": true,
                        "close_opened_onclear" : true
                    },
                    "plugins" : ["search", "sort", "json_data"]
                });
like image 179
Pramod Alagambhat Avatar answered Nov 08 '22 07:11

Pramod Alagambhat


Apart from setting core.check_callback to true, it's worth to mention that creating a simple doesn't require so many arguments. You could simply achieve it by doing:

$('#tree').jstree(true).select_node('nodeid');
var tr = $('#tree').jstree(true),
            selected = tr.get_selected();
selected = tr.create_node(selected, {"type":"file(or any other type you need)"});

Hope this could help someone. I had the problem and tried to follow the API but couldn't work;instead i inspected the codes from demos and found that.

like image 28
tartaruga_casco_mole Avatar answered Nov 08 '22 07:11

tartaruga_casco_mole


I don't know what is causing this, but adding a setTimeout when creating the node fixes the problem

setTimeout(function() {
    $("#tree").jstree("create_node", $("node_1"), "after", { "data": "Hello World" });
}, 1000);
like image 1
Catalin Avatar answered Nov 08 '22 08:11

Catalin


Demo simple what you need http://jsfiddle.net/gwxTa/2/ or http://jsfiddle.net/gwxTa/ or Dynamic - (click add button) http://jsfiddle.net/VBSJ8/ or http://jsfiddle.net/ak4Ed/

Please see my old post: jsTree not working

code from dynamic add button functionality:

$(function() {
    $("#tree").jstree({
        "json_data": {
            "data": [
                {
                "data": "Hello",
                "attr": {
                    "id": "root.id"
                },
                "children": [{
                    "data": "Hello World",
                    "attr": {
                        "id": "node_1"
                    },
                    "children": []}
                  ]},
                ]
        },
        "plugins": ["themes", "json_data", "crrm"]
    });
});


    $("#tree").jstree("create", $("#node_1"), "inside", {
        "data": "child2"
    }, function() {
        alert("added");
    }, true);

Hope you are including the scripts:

<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>

  <link rel="stylesheet" type="text/css" href="/css/normalize.css">
  <link rel="stylesheet" type="text/css" href="/css/result-light.css">

  <script type='text/javascript' src="http://static.jstree.com/v.1.0pre/jquery.jstree.js"></script>

 <script type='text/javascript' src="http://static.jstree.com/v.1.0pre/jquery.hotkeys.js"></script>
like image 1
Tats_innit Avatar answered Nov 08 '22 08:11

Tats_innit