Javascript 如何以编程方式在 jsTree 中选择一个节点并打开所有父节点

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

How do I programmatically select a node in jsTree and open all parents

javascriptjqueryjquery-uijstree

提问by David Hayes

In a multi-level jsTree how do I select a particular node (probably a leaf node) and expand all it's parents? Example:
From this JSFiddle (http://jsfiddle.net/mmeah/fyDE6/) I want to programmatically select Grand Child and have all parent nodes opened.

在多级 jsTree 中,如何选择特定节点(可能是叶节点)并展开它的所有父节点?示例:
从这个 JSFiddle ( http://jsfiddle.net/mmeah/fyDE6/) 我想以编程方式选择 Grand Child 并打开所有父节点。

For some context I'm trying to ensure the user returns to the correct node in the tree if they follow a deep link into my site

对于某些上下文,我试图确保用户返回到树中正确的节点,如果他们跟随一个深层链接进入我的网站

采纳答案by MMeah

jsTree gives an open_node() function to arbitrarily trigger any node to open. Just scan the tree for non-open parents and open them.

jsTree 提供了一个 open_node() 函数,可以任意触发任意节点打开。只需扫描树中未打开的父项并打开它们。

Example: http://jsfiddle.net/mmeah/yyy8W/

示例:http: //jsfiddle.net/mmeah/yyy8W/

$("#findChild").click(function(){
    $.jstree._reference(myTree).open_node("#Node_001",function(){;},false);
});
$("#findGrandChild").click(function(){
    var closedParents = $("#Node_003").parents("li.jstree-closed");
    for(var i=closedParents.length-1;i>=0;i--){
        pleaseOpen($(closedParents[i]));
    }
});

function pleaseOpen(thisNode){
    if(typeof thisNode=="undefined") return;
    if(thisNode.hasClass("jstree-leaf") || thisNode.hasClass("jstree-open") ) return;
    $.jstree._reference(myTree).open_node(thisNode,function(){;},true);
}

?

?

回答by David Hayes

Ah ha, I was on the right track but I had a race condition between my deep linking parsing code and the construction of the tree

啊哈,我是在正确的轨道上,但我的深层链接解析代码和树的构建之间存在竞争条件

To select a node and trigger the event

选择节点并触发事件

$("#tree").jstree("select_node", selector).trigger("select_node.jstree");

To do this after the tree has loaded so it works...

在树加载后执行此操作,使其工作...

$("#tree").jstree(...).bind("loaded.jstree", function () 
{
    $("#tree").jstree("select_node", selector).trigger("select_node.jstree");
});