javascript jstree jquery plugin - 获取父节点的所有子节点和子节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11511858/
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
jstree jquery plugin - Get all child and sub child nodes of parent
提问by Sid
I am using the jsTree jquery plugin
我正在使用jsTree jquery 插件
I am trying to fetch all the nodes and sub nodes of a selected parent. But somehow it's not working unless I use recursion. Is there a better way inherent to jsTree?
我正在尝试获取选定父节点的所有节点和子节点。但不知何故,除非我使用递归,否则它不起作用。jsTree 是否有更好的方法?
采纳答案by Sid
I found a way to do this. In my case I am using xml as the datasource for the tree. I am binding the open_node event to the jstree in the following manner. Mind it that even though I am using xml, the internal structure is that of html data.
我找到了一种方法来做到这一点。就我而言,我使用 xml 作为树的数据源。我通过以下方式将 open_node 事件绑定到 jstree。请注意,即使我使用的是 xml,内部结构也是 html 数据的结构。
// jsTree Configuration hash
var jsTreeConfig = {};
$("#demo1").jstree( jsTreeConfig )
.bind('open_node.jstree', function( e, data ) {
var parentObj = data.rslt.obj; // parent object
var jstreeInstance = data.inst; // jstree instance
$(data.rslt.obj).find("li").each( function( idx, listItem ) {
var child = $(listItem); // child object
// do Stuff with child which can be any level of hierarchy depth
// ...
});
});
回答by Arkadiusz 'flies' Rzadkowolski
You can get full tree all by using this selector: $("#demo1").find("li > a")
您可以使用此选择器获得完整的树: $("#demo1").find("li > a")
回答by usr4896260
I tried the previous solution and it is not working with the latest version of jsTree (v3.2.1). Below is an updated solution to get the child nodes and sub nodes of the parent.
我尝试了以前的解决方案,但它不适用于最新版本的 jsTree (v3.2.1)。以下是获取父节点的子节点和子节点的更新解决方案。
$("#myTree").bind('selected_node.jstree', function (node, data) {
var selectedNodes = $("#myTree").jstree(true).get_json(data.node.id, { flat: true });
for (var i = 0; i < selectedNodes.length; i++) {
// Apply logic here
// ...
// ...
// ...
}
});