How to retrieve all the child nodes at a certain level in jsTree?

2,476 views
Skip to first unread message

Manjunath P T

unread,
Aug 21, 2015, 5:34:36 PM8/21/15
to jsTree
Hi Ivan, first of all thanks for the jsTree plugin, it's a pretty useful.

Q: How to retrieve all the child nodes at a certain level? Say, below is how my tree looks like.

-- Folder1                  // level 1
     -- SubFolder1       // level 2
         --A1                  // level 3
         --B1                  // level 3
     -- SubFolder2       // level 2
         --A2                  // level 3
         --B2                  // level 3
-- Folder2                  // level 1
     --SubFolder2        // level 2


If I select Folder1, I should be able to get all its children ( In this case its SubFolder1 and SubFolder2). Like wise if I select SubFolder2, I should get A2 and B2.
How to do that?

Thanks in advance.


Ivan Bozhanov

unread,
Aug 21, 2015, 5:44:42 PM8/21/15
to jsTree
Use the get_node function - it will return an object which will have a "children" property, which is an array of IDs - those are the immediate children of that node.

For example:
$('button').on('click', function () {
    var instance = $('#tree').jstree(true);
        selected = instance.get_selected()[0];
    console.log(instance.get_node(selected).children);
});

If you want to do that in an event handler - it is even easier:
$('#tree').on('changed.jstree', function (e, data) {
    console.log(data.instance.get_node(data.selected[0]).children);
});

Best regards,
Ivan
Reply all
Reply to author
Forward
0 new messages