Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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() });
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...