Javascript jsTree:如何将选定节点的 ID 获取到 jsTree 中的根节点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10140057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:02:25  来源:igfitidea点击:

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

javascriptjqueryhtmljquery-uijstree

提问by StackOverFlow

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

如何将选定节点的 ID 获取到 jsTree 中的根节点?

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

假设选择了 C 节点,那么如何获取 C 的所有父 ID。

A

一种

  • B

    • C

      +C1

      +c2

    • C

      +C1

      +c2

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

以下代码将仅返回直接父 ID:如果我选择C那么我只得到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

Selected node = C

Parent of Selected node = B

Parent of Selected node = B

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

有没有办法让所有父节点 ID ie Selected node 到 root node ?

  • How to get all child nodes of selected node in jsTree ?
  • 如何在jsTree中获取选定节点的所有子节点?

Any help or guidance in this matter would be appreciated.

在这方面的任何帮助或指导将不胜感激。

回答by mattytommo

Use parentsin jQuery to get all parents, filtering out by libecause all tree items are liin jstree, try this:

parents在 jQuery 中使用获取所有父项,过滤掉,li因为所有树项都li在 中jstree,试试这个:

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

And for children use childrenin jQuery, like so:

对于children在 jQuery 中使用的儿童,如下所示:

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

回答by StackOverFlow

1 More easy solution

1 更简单的解决方案

 .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.

以 ID 数组或节点名称数组的形式返回节点的路径。混合节点:这可以是指向树中元素的 DOM 节点、jQuery 节点或选择器,我们想要其路径。bool id_mode :如果设置为 true,则返回 ID 而不是父节点的名称。默认为假。

// 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);