javascript jsTree:刷新后如何选择节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6326976/
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
jsTree: how to select node after refresh
提问by Glen
I have a jQuery jsTree populated from the server via an ajax call. When I add a new node I make an ajax call then make a call to refresh the tree with tree.jstree("refresh")
. After the refresh I want to select the node I just added. Unfortunately there doesn't seem to be a callback that can be passed to this command. Is there any clean way to do this?
我有一个通过 ajax 调用从服务器填充的 jQuery jsTree。当我添加一个新节点时,我进行了 ajax 调用,然后调用以使用tree.jstree("refresh")
. 刷新后我想选择我刚刚添加的节点。不幸的是,似乎没有可以传递给此命令的回调。有没有干净的方法来做到这一点?
采纳答案by Adi
oh, such a long time since this post ... and still couldn't find an answer on internet. So after a few hours of ... no no no, not this, came up with a solutin
哦,这篇文章已经很久了......仍然无法在互联网上找到答案。所以经过几个小时的......不不不,不是这个,想出了一个解决方案
var jsTreeId = '#jstree'; // or whatever name the jstree has
var jsTreeSelectedItemId = 5; // just an example
var selectedNode = $('#node_'+jsTreeSelectedItemId);
var parentNode = $.jstree._reference(jsTreeId)._get_parent(selectedNode);
// now lets say that you add a new node from server side, you get the new id of the created node by an ajax call, and next you want to refresh the tree in order to display it, and also select it
// 现在假设您从服务器端添加一个新节点,您通过 ajax 调用获取创建的节点的新 id,接下来您要刷新树以显示它,并选择它
var newSelectId = 9; // or from ajax call
// call the refresh function, which is asnyc
$.jstree._reference(jsTreeId).refresh(parentNode);
// set the magic "to_select" variable with an array of node ids to be selected
// note: this must be set after refresh is called, otherwise won't work
$.jstree._reference(jsTreeId).data.ui.to_select = ['#node_'+newSelectId];
回答by Rikin Patel
$('#tree').jstree("select_node", '#1', true);
//other node are deselected if pass last argument as true.
$('#tree').jstree("select_node", '#1', false);
//other node are selected and new one also selected.