jsTree - render all nodes before they are expanded?

南笙酒味 提交于 2020-01-02 11:26:08

问题


I'm trying to display the titles of all descendants of a jsTree parent in a separate div (.childNodes) when an item is selected in the tree. My issue is that the descendants are seemingly being lazy loaded, and are only rendered in the DOM when a node is expanded, not when it is selected. Is there a way to disable this to render all nodes before the parent is expanded?

This is how I'm creating the tree:

container.jstree({
'core': {
    'data': {
        'url': nodesContent,
    }
}});

And this is how I'm handling the selected event:

container.on("changed.jstree", function (e, data) {
var i,
    list = "<ul>";
var children = data.instance.get_children_dom(data.selected[0]);
for (i = 0; i < children.length; i++) {
    list += "<li>" + children[i].innerHTML + "</li>";
}
list += "</ul>";
jQuery(".childNodes").html(list);});

Thank you.


回答1:


No it can not be disabled - only visible nodes are kept in the DOM, you can however do what is recommended in the docs and work with the internal jstree model to retrieve the titles.

container.on("changed.jstree", function (e, data) {
    var i,
        list = "<ul>";
        children = data.instance.get_node(data.selected[0]).children;
    for (i = 0; i < children.length; i++) {
        list += "<li>" + data.instance.get_node(children[i]).text + "</li>";
    }
    list += "</ul>";
    jQuery(".childNodes").html(list);
});

If you want to display all descendants (not only immediate) substitute:
data.instance.get_node(data.selected[0]).children
with:
data.instance.get_node(data.selected[0]).children_d



来源:https://stackoverflow.com/questions/31654375/jstree-render-all-nodes-before-they-are-expanded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!