[英]jsTree : How to get IDs of selected nodes to root node in jsTree?
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 C
+C1 + C1
+c2 + 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
Parent of Selected node = B
Is there any way to get all parent nodes ID ie Selected node to root node ? 有没有办法获取所有父节点ID,即选择节点到根节点?
Any help or guidance in this matter would be appreciated. 任何有关此事的帮助或指导将不胜感激。
Use parents
in jQuery to get all parents, filtering out by li
because all tree items are li
in jstree
, try this: 在jQuery中使用parents
来获取所有父项,由li
过滤掉,因为所有树项都是jstree
中的li
,试试这个:
var parents = data.rslt.obj.parents("li");
And for children use children
in jQuery, like so: 对于孩子children
在jQuery中使用children
,就像这样:
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() });
});
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. 将路径返回到节点,可以是ID数组,也可以是节点名数组。 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. mixed node:这可以是指向树中元素的DOM节点,jQuery节点或选择器,我们想要它的路径.bool id_mode:如果设置为true,则返回ID而不是父节点的名称。 Default is false. 默认值为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.