繁体   English   中英

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

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

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

假设C是选定节点然后我如何获得C的所有父ID。

一种

    • C

      + C1

      + C2

以下代码将仅返回直接父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"))
 });

输出:

Selected node = C

Parent of Selected node = B

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

  • 如何在jsTree中获取所选节点的所有子节点?

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

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

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

对于孩子children在jQuery中使用children ,就像这样:

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

编辑使用上面的内容,这里是如何获取所有父级和子级,并将它们放在每个的所有数组中:

父母:

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

儿童:

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

1更简单的解决方案

 .get_path ( node , id_mode )

将路径返回到节点,可以是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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM