Search code examples
jquerynodesjstreechildren

jsTree - render all nodes before they are expanded?


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.


Solution

  • 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