简体   繁体   English

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

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

In a multi-level jsTree how do I select a particular node (probably a leaf node) and expand all it's parents?在多级 jsTree 中,如何选择特定节点(可能是叶节点)并展开它的所有父节点? Example:例子:
From this JSFiddle ( http://jsfiddle.net/mmeah/fyDE6/ ) I want to programmatically select Grand Child and have all parent nodes opened.从这个 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在某些情况下,如果用户通过深层链接进入我的网站,我会尝试确保用户返回到树中的正确节点

jsTree gives an open_node() function to arbitrarily trigger any node to open. jsTree 提供了一个 open_node() 函数来任意触发任何节点打开。 Just scan the tree for non-open parents and open them.只需扫描树以查找未打开的父母并打开它们。

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

​​​

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

jQuery selection doesn't work on nodes that are not opened [expanded] already. jQuery 选择不适用于尚未打开 [展开] 的节点。 Hence I chose to open [expand] all elements before I did the jQuery selection and then close all the nodes after that.因此,我选择在进行 jQuery 选择之前打开 [展开] 所有元素,然后关闭所有节点。

 // open all nodes to select the desired node using jquery. $("#tree").jstree('open_all'); var node = $("desiredNodeSelector"); // close all tree since we are done with the selection $("#tree").jstree('close_all');

Now all we need is just use the 'select_node' jstree api.现在我们只需要使用'select_node' jstree api。

 $("#tree").jstree('select_node', node);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM