JsTree: how to select node after update

I have jQuery jsTree populated from the server via an ajax call. When I add a new node, I make an ajax call and then make a call to update the tree with tree.jstree("refresh"). After the update, I want to select the node that I just added. Unfortunately, it seems that feedback cannot be passed to this command. Is there any clean way to do this?

+3
source share
2 answers

oh, such a long time from this post ... and still have not found an answer on the Internet. So after a few hours ... no, no, not that, came up with 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);

// , node , node ajax-, , ,

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];
+3
$('#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.
+1

All Articles