jsTree如何获取选中节点的ID到根节点?
假设 C 是选定的节点,那么如何获取 C 的所有父 ID。
一个
B
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"))
});
输出:
选定节点 = C
所选节点的父节点 = B
有没有办法获取所有父节点 ID,即从选定节点到根节点?
- jsTree中如何获取选中节点的所有子节点?
如有任何帮助或指导,我们将不胜感激。
最佳答案
使用 parents
在 jQuery 中获取所有 parent ,通过 li
过滤掉因为所有树项都是 li
在 jstree
,试试这个:
var parents = data.rslt.obj.parents("li");
child 用children
在 jQuery 中,像这样:
var children = data.rslt.obj.parent().find('li');
编辑使用上面的内容,这里是如何获取所有父子并将它们放入每个数组中的方法:
parent :
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() });
});
关于javascript - jsTree : How to get IDs of selected nodes to root node in jsTree?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10140057/