I have a strange problem with JSTree and Ajax.
I generate my tree using an Ajax / PHP request that generates HTML (with UL, LI, A tags) using ...
$.ajax({ url: 'ajaxTreeView.php?method=edit&partid='+partId, cache: false, success: function(tree) { $('#treeViewer').html(tree); }});
and activate JStree in code using ...
options = { "core": { animation: 120 }, "themes": { theme: 'corral', dots: true }, "types": { "types": { "parent": { "icon": { "image": "images/custom/Legend/node_select.png" } }, "child": { "icon": { "image": "images/custom/Legend/node_select_child.png" } }, "current": { "icon": { "image": "images/custom/Legend/node.png" } } } }, "plugins": [ "html_data", "types", "themes", "ui", "contextmenu", "cookies" ], "ui": { "select_limit" : 1 }, "cookies": { "save_selected" : false } }; $("#tree").jstree(options);
It seems I can not easily select node. I tried initial_select, but that doesn't seem to work, and also not perfect, as I often want to select node programmatically anyway.
I tried using ...
$('#tree').jstree("select_node", '#ref565', true);
This works if I call the function via a hyperlink, but it does not work if I call it after I turn on JStree.
I see from the addition of a warning (all this happens in the Ajax Success procedure) ...
$('#treeViewer').html(tree); $("#tree").jstree(options); alert('test'); $('#tree').jstree("select_node", '#ref565', true);
... that the tree did not appear before the warning starts.
I added setTimeOut ...
$('#treeViewer').html(tree); $("#tree").jstree(options); setTimeout(function() {selectNode(565);},1250); $('#tree').jstree("select_node", '#ref565', true);
... it works.
I'm obviously stupid. Am I using the wrong syntax? Why do I need to set a delay to select a node?
Please, help.