jsTree:刷新后如何选择节点

问题描述

| 我有一个通过ajax调用从服务器填充的jQuery jsTree。当我添加一个新节点时,我进行了ajax调用,然后进行了调用以刷新the0ѭ树。刷新后,我想选择刚添加的节点。不幸的是,似乎没有可以传递给该命令的回调。有什么干净的方法可以做到这一点吗?     

解决方法

        哦,自这篇文章以来已经有这么长时间了...仍然在互联网上找不到答案。 因此,经过几个小时的……不,不,不是这个,想出了一种山梨素
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];
    ,        
$(\'#tree\').jstree(\"select_node\",\'#1\',true);
//other node are deselected if pass last argument as true.

$(\'#tree\').jstree(\"select_node\",false);
//other node are selected and new one also selected.