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

血红的双手。 提交于 2019-11-27 14:51:20

问题


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.


回答1:


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



回答2:


1 More easy solution

 .get_path ( node , id_mode )

return the path to a node, either as an array of IDs or as an array of node names. mixed node : This can be a DOM node, jQuery node or selector pointing to an element within the tree, whose path we want.bool id_mode : If set to true IDs are returned instead of the names of the parents. Default is false.

// To get path [ID or Name] from root node to selected node 

var ids = data.inst.get_path('#' + data.rslt.obj.attr('id'),true);

// Returns IDs from root to selected node

var names = data.inst.get_path('#' + data.rslt.obj.attr('id'),false); 

// Returns Name's from root to selected node 

alert("Path [ID or Name] from root node to selected node = ID's = "+ids+" :: Name's = "+names);


来源:https://stackoverflow.com/questions/10140057/jstree-how-to-get-ids-of-selected-nodes-to-root-node-in-jstree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!