2011-06-13 603 views
3

我有一個jQuery jsTree從服務器通過ajax調用填充。當我添加一個新節點時,我做了一個ajax調用,然後撥打電話以tree.jstree("refresh")刷新樹。刷新後,我想選擇剛剛添加的節點。不幸的是,似乎沒有可以傳遞給此命令的回調。有沒有乾淨的方法來做到這一點?jsTree:如何在刷新後選擇節點

+0

要添加節點作爲最後一個節點或在樹間? – Vivek 2011-06-13 05:50:25

回答

3

哦,這麼長時間以來,這篇文章......仍然無法在互聯網上找到答案。 的......不不不數小時後所以,不是這個,想出了一個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); 

//現在讓我們說,你從服務器端添加一個新的節點,你得到的新ID通過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]; 
1
$('#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.