2012-12-10 36 views
0

我正在使用jstree javascript插件,並且正在異步加載數據。我正試圖在這棵樹的數據上實現搜索。我有一個Web服務,它根據搜索參數返回一個ID數組。搜索前未加載樹。當用戶點擊搜索時,基於返回的ID,我試圖加載/打開樹中對應於ID的節點。如何使用ID數組打開jstree中的節點?

for(var i = 0; i < ids.length; i++){ 
    $("#tree").jstree("open_node", document.getElementById(ids[i])); 
} 

但是,只有根節點正在被加載。任何人都可以提出一個簡單的函數來使用ID數組逐個加載節點嗎?

在此先感謝。

回答

0

如果沒有加載jsTree,那麼在爲jsTree構建數據時,您可以使用「state」=>「open」作爲要在加載時打開的節點。請參閱下面的示例 - 代碼複製從http://www.jstree.com/documentation/json_data

{ 
    "data" : "node_title", 
    // omit `attr` if not needed; the `attr` object gets passed to the jQuery `attr` function 
    "attr" : { "id" : "node_identificator", "some-other-attribute" : "attribute_value" }, 
    // `state` and `children` are only used for NON-leaf nodes 
    "state" : "closed", // or "open", defaults to "closed" 
    "children" : [ /* an array of child nodes objects */ ] 
}