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