1

如何在 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);

但它失败了!孩子不是被创造出来的。

4

1 回答 1

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);
于 2013-02-06T15:08:03.510 回答