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

时间:2021-03-05 18:07:46

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

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

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

  • B C + C1 + c2

Following code will return only immediate parent ID: If I selected C then 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

选定节点= C.

Parent of Selected node = B

所选节点的父节点= B.

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

有没有办法获取所有父节点ID,即选择节点到根节点?

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

Any help or guidance in this matter would be appreciated.

任何有关此事的帮助或指导将不胜感激。

3 个解决方案

#1


16  

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

在jQuery中使用parent来获取所有父项,由li过滤掉,因为所有树项都是jstree中的li,请尝试:

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

And for children use children in jQuery, like so:

对于孩子们在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() });
});

#2


10  

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数组,也可以是节点名数组。 mixed node:这可以是指向树中元素的DOM节点,jQuery节点或选择器,我们想要它的路径.bool id_mode:如果设置为true,则返回ID而不是父节点的名称。默认值为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);

#3


-2  

You can try

你可以试试

data.instance.get_path(data.node,'/',true)

#1


16  

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

在jQuery中使用parent来获取所有父项,由li过滤掉,因为所有树项都是jstree中的li,请尝试:

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

And for children use children in jQuery, like so:

对于孩子们在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() });
});

#2


10  

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数组,也可以是节点名数组。 mixed node:这可以是指向树中元素的DOM节点,jQuery节点或选择器,我们想要它的路径.bool id_mode:如果设置为true,则返回ID而不是父节点的名称。默认值为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);

#3


-2  

You can try

你可以试试

data.instance.get_path(data.node,'/',true)