Menencia
Menencia

Reputation: 1289

jsTree : how to add a complex node

How to add a branch in a jstree instance (ie. a node with childrens of unknown levels).

// example of a branch (with one level)
var branch = {
    "data":"Folder 1",
    "attr":{"rel":"Layer","elt":1},
    "state":"closed",
    "children":[
        {
            "data":"Item 4",
            "attr":{"rel":"File 1","elt":2},
            "state":"",
            "children":null
        }
    ]
};

And to add this branch in a jstree instance :

// -1 means root
$.jstree._reference('#tree').create_node(-1, 'last', branch, false, false);

But it fails ! The children are not created.

Upvotes: 1

Views: 991

Answers (1)

Menencia
Menencia

Reputation: 1289

My solution is to create all the nodes manually and recursively :

function LoadElement(node, branch) {
    data = {
        data: branch.name,
        attr: branch.attr,
        state: branch.state
    };
    var node = $.jstree._reference('#tree').create_node(
        node, 'last', data, false, false
    );
    if (typeof branch.children === 'undefined') return false;
    for (var i=0; i<branch.children.length; i++) {
        LoadElement(node, branch.children[i]);
    }
}

// -1 means root
LoadElement(-1, branch);

Upvotes: 1

Related Questions