Javascript jsTree 打开一个分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4171111/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:13:54  来源:igfitidea点击:

jsTree Open a branch

javascriptjquerytreeviewjstree

提问by RailsSon

I am using the JQuery plugin jsTree, http://www.jstree.com/I am able to expand the whole tree with the following method:

我正在使用 JQuery 插件 jsTree,http://www.jstree.com/我可以使用以下方法扩展整个树:

$("#tree").jstree("open_all");

and also a specific node:

还有一个特定的节点:

$("#tree").jstree("open_node", $('#childNode'));

I am having difficulty opening a branch of the tree, open branch opens it fine but does not open its parent if it has one.

我在打开树的一个分支时遇到困难,打开的分支可以很好地打开它,但如果它有一个,则不会打开它的父级。

Has anyone successully done this with jsTree? Let me know if you need more info.

有没有人用 jsTree 成功地做到了这一点?如果您需要更多信息,请告诉我。

Thanks

谢谢

Eef

埃夫

采纳答案by Bob

You could use the binding

你可以使用绑定

$("#tree").bind("open_node.jstree", function (event, data) { 
  if((data.inst._get_parent(data.rslt.obj)).length) { 
    data.inst._get_parent(data.rslt.obj).open_node(this, false); 
  } 
}); 

回答by KiriLL Ivanov

Your code for open branch is correct.

您的开放分支代码是正确的。

For example. Source of tree:

例如。树的来源:

    <div id="treeTask">
       <ul>
          <li id="node_37"><a href="#">TEST1</a>
              <ul>
                  <li id="node_38"><a href="#">TEST2</a></li>
                  <li id="node_39"><a href="#">TEST3</a></li>
              </ul>
          </li>
      </ul>
   </div>

Open node:

打开节点:

$("#treeTask").jstree("open_node", $("#node_38"));

回答by Arvind

Try this code to open node till nth Level

尝试使用此代码打开节点直到第 n 级

$("#myTree").jstree({options}).bind('loaded.jstree', function (e, data) {
    /** 
     * Open nodes on load (until x'th level) 
     */
    var depth = 3;
    data.inst.get_container().find('li').each(function (i) {
        if (data.inst.get_path($(this)).length <= depth) {
            data.inst.open_node($(this));
        }
    });
});

回答by Jeremy K

Here is function that can open a specific node and all its parents.

这是可以打开特定节点及其所有父节点的函数。

function expandNode(nodeID) {
    // Expand all nodes up to the root (the id of the root returns as '#')
    while (nodeID != '#') {
        // Open this node
        $("#jstree").jstree("open_node", nodeID)
        // Get the jstree object for this node
        var thisNode = $("#jstree").jstree("get_node", nodeID);
        // Get the id of the parent of this node
        nodeID = $("#jstree").jstree("get_parent", thisNode);
    }
}

回答by Manfred Wuits

i found Ted's code working, but had to change it a bit:

我发现 Ted 的代码有效,但不得不对其进行一些更改:

 $('#jsTree').bind("open_node.jstree", function (event, data) { 
      if((data.inst._get_parent(data.rslt.obj)).length) { 
        data.inst.open_node(data.inst._get_parent(data.rslt.obj), false,true); 
      } 
    });

回答by dzona

None of previous worked for me, so I have created this code, and it works like a charm :)

以前没有一个对我有用,所以我创建了这段代码,它就像一个魅力:)

$('#tree').on('open_node.jstree', function (event, data) { 
    if(data.node.parent !== "#") { 
        data.instance.open_node(data.node.parent); 
    } 
});

回答by Miro Margalitadze

just use this if you use json

如果您使用 json,请使用它

$("#treeId").on
('loaded.jstree', function() {
 $("#treeId").jstree("open_node", $("#nodeId"));
 });

回答by dvpweb

    // Expand pasted, dragged and dropped node for jstree 3.3.1
        var objtree = $('#jstree');
        objtree.bind('paste.jstree', function(e, d) { objtree.jstree('open_all', '#' + d.parent); });
        $(document).bind('dnd_move.vakata', function(e, d) { objtree.jstree('open_all', $(d.event.target).closest('.jstree-node').attr('id')); });
        $(document).bind('dnd_stop.vakata', function(e, d) { objtree.jstree('open_all', $(d.event.target).closest('.jstree-node').attr('id')); });