How can I tell if jsTree has fully loaded?

In the more recent versions of jstree, you may need to wait until all the nodes have finished loading before interacting with them. To do this you need:

ready.jstree

So:

$(selector)
    .bind('ready.jstree', function(e, data) {
        // invoked after jstree has loaded
     })
...

I used setInterval and clearInterval:

var interval_id = setInterval(function(){
     // $("li#"+id).length will be zero until the node is loaded
     if($("li#"+id).length != 0){
         // "exit" the interval loop with clearInterval command
         clearInterval(interval_id)
         // since the node is loaded, now we can open it without an error
         $("#tree").jstree("open_node", $("li#"+id))
      }
}, 5);

JStree's ".loaded" callback only works for root nodes; "._is_loaded" may work instead of checking the node's length, but I haven't tried it. Either way, the animation settings cause nodes that are deeper in the tree to be loaded a few milliseconds later. The setInterval command creates a timed loop that exits when your desired node(s) is loaded.


Before you call .jstree() on an element, you can bind your callbacks to before.jstree and loaded.jstree events:

$(selector)
.bind('before.jstree', function(e, data) {
    // invoked before jstree starts loading
})
.bind('loaded.jstree', function(e, data) {
    // invoked after jstree has loaded
    $(this).jstree("open_node", $(nodes[i]));
})
.jstree( ... )

You had better call $.ajax() function to get data you want at first, you can call $().jstree() function in the success: field like bellow my code.

    var fullTree;
$.ajax({
  url :"./php/select_tree.php",
  data : fullTree,
  method : "GET",
  dataType : "json",
  success : function(fullTree){
    $("#jstree").jstree({
      "core" : {
        "data" :fullTree,
        "check_callback" : true
       },
       "plugins" : [ "contextmenu", "dnd", "changed", "wholerow" ]
    });
  }
})
;