Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
162
rated 0 times [  167] [ 5]  / answers: 1 / hits: 28530  / 12 Years ago, fri, april 13, 2012, 12:00:00

How to get IDs of selected nodes to root node in jsTree?



Assume C is selected node then How can I get All parent IDs of C.



A




  • B




    • C



      +C1



      +c2





Following code will return only immediate parent ID:
If I selected C then I get only B



 .bind(select_node.jstree, function (event, data) {  
//`data.rslt.obj` is the jquery extended node that was clicked
alert(Selected node = + data.rslt.obj.attr(id));
alert(Parent of Selected node = + data.inst._get_parent(data.rslt.obj).attr(id))
});


Output:



Selected node = C



Parent of Selected node = B



Is there any way to get all parent nodes ID i.e. Selected node to root node ?




  • How to get all child nodes of selected node in jsTree ?



Any help or guidance in this matter would be appreciated.


More From » jquery

 Answers
19

Use parents in jQuery to get all parents, filtering out by li because all tree items are li in jstree, try this:



var parents = data.rslt.obj.parents(li);


And for children use children in jQuery, like so:



var children = data.rslt.obj.parent().find('li');


EDIT Using the above, here's how to get all parent and children and put them in all an array for each:



Parents:



var parents = [];
data.rslt.obj.parents(li).each(function () {
parents.push({ id: $(this).attr(id), description: $(this).children(a).text() });
});


Children:



var children = [];
data.rslt.obj.find(li).each(function () {
children.push({ id: $(this).attr(id), description: $(this).children(a).text() });
});

[#86258] Thursday, April 12, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marisela

Total Points: 103
Total Questions: 105
Total Answers: 102

Location: Solomon Islands
Member since Fri, Oct 8, 2021
3 Years ago
;