2012-02-16 469 views
1

我正在使用Extjs4 treepanel和Json數據。我想重新加載treepanel並需要展開樹形菜單到指定節點。 我正在使用下面的代碼。請讓我知道這是如何做到的。Extjs 4 treepanel重新加載並展開

//Define tree store 

var store = Ext.create('Ext.data.TreeStore', { 
    proxy: { 
     type: 'ajax', 
     url: 'my_tree.php', 
    }, 

    noCache: false 
}); 

// Treepanel 
var treePanel = Ext.create('Ext.tree.Panel', { 
    id: 'mytree', 
    renderTo: 'tree_div', 
    height: 400, 
    bodyBorder: false, 
    border: false, 
    singleExpand: true, 
    rootVisible: false, 
    store: store, 
    useArrows: true 
}); 

//Reload tree 
    function reload_tree(){ 
     var tree_panel = Ext.getCmp('mytree'); 
     tree_panel.enable(); 
     tree_panel.getLoader().dataUrl = 'my_tree.php'; 
     tree_panel.getLoader().load(tree_panel.root); 
     //Expand tree node 
     tree_panel.getLoader().on('load', function(loader, node){ 
       tree_panel.getNodeById(nodeid).expand(); 
    //here node id is tree menu nodeid 
    }); 
    } 

在此先感謝

回答

6

刷新樹我認爲這是簡單到只需要調用

treePanel.getStore().load(); 

,那麼你可以聽加載事件,這樣的數據是所有取終於從那裏選擇所需的節點。

treePanel.on("load", function(treeStore, records, successful, operation)){ 

    var id = 4; // This is the ID of the node that somehow you know in advance 
    var node = treeStore.getNodeById(id); 

    treePanel.expandPath(node.getPath()); 
}); 

HTH!

+0

感謝您的快速回放。treepanel加載正常,但它不擴展,在螢火蟲顯示錯誤「node.getPath不是功能」。 – siva565 2012-02-16 12:33:03

+0

哦,我的壞,對不起,getPath()消失在ExtJS 4,雖然你有和替代方法來擴大所需的節點,請看這個帖子中的分子人的答案http://stackoverflow.com/questions/6709106/通過發送內部參數作爲參數 – 2012-02-16 12:43:22

+0

humm來感謝Byte slave非常感謝。它工作正常。該節點完全展開我期待的內容,但未被選中(風格影響)。一般來說,如果選擇的樹木節點有一些不同的選擇風格。你可以幫我嗎? – siva565 2012-02-16 12:54:03