使用treeStore ExtJS(代理解决方案)时选择节点

时间:2013-10-16 09:13:17

标签: extjs extjs4 extjs4.1 extjs4.2

我有树店。

var store = Ext.create('Ext.data.TreeStore', {
    root: {
        autoLoad:false,
        expanded: false,
        children: [
            { 
                id:"0", text: "School Friends", expanded: true, children: 
                [
                    {
                        id:"1", text: "Mike", leaf: true, name: "Mike", email: "mike@stackoverflow.com", phone: "345-2222"
                    },
                    {
                        id:"select", text: "Laura", leaf: true, name: "Laura", email: "laura@stackoverflow.com", phone: "345-3333"
                    }
                ] 
            }

        ]
    }
});

和树小组。

Ext.create('Ext.tree.Panel', {
    title: 'Simple Tree',
    width: 200,
    height: 150,
    store: store,
    rootVisible: false,
    renderTo: Ext.getBody(),
    listeners:{
        afterrender:function(){
          var record = this.getStore().getNodeById('1');
          this.getSelectionModel().select(record)
        }
    }
});

一切正常!但是当我更改商店时,使用代理请求。选择是n * 工作 *

    var storeTree = Ext.create('Ext.data.TreeStore', {
    autoLoad:true,
    expanded: true,

    proxy: {
        type: 'ajax',
        url: 'tree.json',
    },

    root: {
        text: 'Ext JS',
        id: 'src',
        expanded: true,
      // children:[]
    },

    folderSort: true,
    sorters: [{
        property: 'text',
        direction: 'ASC'
    }]
});

我使用相同的 JSON

[

    {"id":4, "text":"second",},

    {
    id:"0", text: "School Friends", expanded: true, children: 
                [
                    {
                        id:"1", text: "Mike", leaf: true, name: "Mike", email: "mike@stackoverflow.com", phone: "345-2222"
                    },
                    {
                        id:"select", text: "Laura", leaf: true, name: "Laura", email: "laura@stackoverflow.com", phone: "345-3333"
                    }
                ] 

    },

]

1 个答案:

答案 0 :(得分:0)

例如一个解决方案:

 var storeTree = Ext.create('Ext.data.TreeStore', {
    autoLoad:false,
    expanded: false,

    proxy: {
        type: 'ajax',
        url: 'tree2.json',

        extraParams: {o: 'SELECTED'},
    },

    root: {
        text: 'Ext JS',
        id: 'src',
        expanded: true,
        children:[]
    },

    folderSort: true,
    sorters: [{
        property: 'text',
        direction: 'ASC'
    }]
});


storeTree.load({
    url: 'tree.json'
});


 storeTree.on({
    'load': function(store) {
        var node = store.getNodeById("select"); // your id here

        treePanel.getSelectionModel().select([node]);
        treePanel.selectPath(node.getPath());
    },
    single: true
});