jsTree:如何添加复杂节点

时间:2013-02-06 15:08:04

标签: jstree

如何在jstree实例中添加分支(即具有未知级别子节点的节点)。

// 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
        }
    ]
};

并在jstree实例中添加此分支:

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

但它失败了!孩子们没有被创造出来。

1 个答案:

答案 0 :(得分:1)

我的解决方案是手动和递归地创建所有节点:

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);