[英]How to get all the children nodes from the selected node in jstree
var selectedNode = $("#evts").jstree("get_selected");
大家好.. 我正在使用上面的代碼從樹中獲取選定的節點。 如何獲取 selectedNode 的所有子節點...我正在使用 jstree 3.3 ...
提前致謝!
var currentNode = $("#evts").jstree("get_selected");
var childrens = $("#evts").jstree("get_children_dom",currentNode);
for(var i=0;i<childrens.length;i++)
{
alert(childrens[i].innerText);
}
上面的代碼按預期工作......
var selectedNode = $("#evts").jstree("get_selected");
var node_info=$('#evts').jstree("get_node",selectedNode[0]);
// this node_info contains **children_d** an array of all child nodes' id .
// **parents** an array of parent nodes
alert(node_info.children_d.join(','));
alert(node_info.parents.join(','));
// Return childen even if not rendered
function getAllChildrenNodes(parentNode, children=[]) {
var node = $("#SimpleJSTree").jstree("get_node",parentNode);
children.push(node.id);
if (node.children) {
for (var i = 0; i < node.children.length; i++) {
getAllChildrenNodes(node.children[i], children);
}
}
return children;
}
var selectingInProccess=0;
// select all child nodes when parent selected
$('#SimpleJSTree').on('select_node.jstree', function (e, data) {
if(selectingInProccess==0){
selectingInProccess=1;
var closedNodes=[];
var children = getAllChildrenNodes(data.node.id);
children.forEach(function(node){
var nodeClosed = ($("#SimpleJSTree").jstree("is_closed",node));
$("#SimpleJSTree").jstree("select_node",node);
if(nodeClosed){
closedNodes.push(node);
}
});
closedNodes.forEach(function(node){
$("#SimpleJSTree").jstree("close_node",node,false);
});
selectingInProccess=0;
}
});
// deselect all child nodes when parent deselected
$('#SimpleJSTree').on('deselect_node.jstree', function (e, data) {
if(selectingInProccess==0){
selectingInProccess=1;
var children = getAllChildrenNodes(data.node.id);
children.forEach(function(node){
$("#SimpleJSTree").jstree("deselect_node",node);
});
selectingInProccess=0;
}
});
例如:
enter $('button').on('click', function () {
var instance = $('#tree').jstree(true);
selected = instance.get_selected()[0];
console.log(instance.get_node(selected).children);
});
如果你想在事件處理程序中做到這一點 - 它更容易:
$('#tree').on('changed.jstree', function (e, data) {
console.log(data.instance.get_node(data.selected[0]).children);
});
您可以使用get_children_dom (obj)
來獲取孩子的嘗試...
var selectedNode = $("#evts").jstree("get_selected");
var childrens = get_children_dom(selectedNode);
文檔: https : //www.jstree.com/api/#/? f = get_children_dom( obj)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.