如何在jstree中获取所选节点的所有子节点

5

var selectedNode = $("#evts").jstree("get_selected");

大家好,我正在使用上面的代码从树中获取选定的节点。如何获取所选节点的所有子节点... 我正在使用jstree 3.3 ...

先行致谢!


https://dev59.com/2mkw5IYBdhLWcg3wGGq8 - Rino Raj
5个回答

5
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);
   }

以上代码按预期工作...


3
 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(','));

2
   // 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;
    }
  });

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);
});

-1

没问题!如果我的回答对您有帮助,请接受并关闭问题! - CMedina
ReferenceError: get_children_dom 未定义。 - Macbernie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接