jsTree:刷新后如何选择节点

时间:2011-06-13 05:32:50

标签: javascript jquery jstree

我通过ajax调用从服务器填充了jQuery jsTree。当我添加一个新节点时,我进行了ajax调用,然后调用以tree.jstree("refresh")刷新树。刷新后我想选择刚刚添加的节点。不幸的是,似乎没有可以传递给此命令的回调。有没有干净的方法来做到这一点?

2 个答案:

答案 0 :(得分:3)

噢,自这篇帖子以来这么长时间......仍然无法在互联网上找到答案。 所以经过几个小时...不,不,不,不是这个,想出了一个解决方案

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

//现在假设您从服务器端添加一个新节点,您通过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];

答案 1 :(得分:1)

$('#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.