I am using the jsTree jquery plugin
I am trying to fetch all the nodes and sub nodes of a selected parent. But somehow it's not working unless I use recursion. Is there a better way inherent to jsTree?
I am using the jsTree jquery plugin
I am trying to fetch all the nodes and sub nodes of a selected parent. But somehow it's not working unless I use recursion. Is there a better way inherent to jsTree?
Share Improve this question edited Jul 16, 2012 at 20:28 Santiago Elvira Ramirez 1,1918 silver badges28 bronze badges asked Jul 16, 2012 at 20:15 SidSid 4,99514 gold badges61 silver badges114 bronze badges 1- P.S. - I have answered my question here. – Sid Commented Jul 17, 2012 at 16:31
3 Answers
Reset to default 4You can get full tree all by using this selector: $("#demo1").find("li > a")
I tried the previous solution and it is not working with the latest version of jsTree (v3.2.1). Below is an updated solution to get the child nodes and sub nodes of the 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
// ...
// ...
// ...
}
});
I found a way to do this. In my case I am using xml as the datasource for the tree. I am binding the open_node event to the jstree in the following manner. Mind it that even though I am using xml, the internal structure is that of 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
// ...
});
});