Jquery – JsTree Open a node then select a child node (using json_result)

asp.net-mvc-2jqueryjstree

I am having trouble with a JsTree I am using in an MVC2 project. I would like to create a function to deselect/close all nodes on the tree. Then open a specific node, and select a specific child node (I have the Id values for both).

The trouble is that the select_node is always called before the open_node finishes, so the node is not selected, as the tree has not loaded the data yet, and the node ID does not exist.

I first tried this function.

$('#demo3').jstree('deselect_all');
$('#demo3').jstree('close_all');
$('#demo3').jstree("open_node", $('#ParentId'), false, true); 
$('#demo3').jstree("select_node", $('#ChildId'));

I then tried moving the code to the select_node and move_node binds of the tree, but no luck. At the moment I'm stuck using a setTimeout(), which is a horrible solution.

Does anyone know how I can tell the tree to only select the node after opening has finished?

Best Answer

You could try passing a function that selects the node as a callback like:

$('#demo3').jstree('open_node', '#ParentID', function(e, data) {
    $('#demo3').jstree('select_node', '#ChildId');
}, true);

This way select_node will be called once the open_node returns success.

Related Topic