Jstree jquery plugin - Get all child and child nodes of parent

I am using jsTree jquery plugin

I am trying to retrieve all nodes and sub nodes of the selected parent. But for some reason this does not work if I do not use recursion. Is there a better way inherent to jsTree?

+5
source share
3 answers

I found a way to do this. In my case, I use xml as the data source for the tree. I bind the open_node event with jstree as follows. Keep in mind that although I use xml, the internal structure is the html data.

// jsTree Configuration hash
var jsTreeConfig = {};

$("#demo1").jstree( jsTreeConfig )
            .bind('open_node.jstree', function( e, data ) {
                var parentObj = data.rslt.obj; // parent object
                var jstreeInstance = data.inst; // jstree instance
                $(data.rslt.obj).find("li").each( function( idx, listItem ) {
                    var child = $(listItem); // child object
                    // do Stuff with child which can be any level of hierarchy depth
                    // ...
                });
            });
+2
source

You can get the full tree using this selector: $("#demo1").find("li > a")

+4
source

I tried the previous solution and did not work with the latest version of jsTree (v3.2.1). The following is an updated solution for getting child nodes and child nodes of a parent.

$("#myTree").bind('selected_node.jstree', function (node, data) {
    var selectedNodes = $("#myTree").jstree(true).get_json(data.node.id, { flat: true });
    for (var i = 0; i < selectedNodes.length; i++) {
       // Apply logic here
       // ...
       // ...
       // ...
    }
});
+4
source

All Articles