2013-03-20 77 views
1

我試圖顯示不同的數據(來自不同的商店和模型)按鈕點擊,但在某些時候失敗(坦率地說,我不知道如何使用重新配置功能)。下面是我的代碼:靜態網格​​與按鈕點擊在extjs4動態數據

組件和按鈕點擊功能:

Ext.define('MyApp.view.MyPanel', { 
    extend: 'Ext.panel.Panel', 

    height: 328, 
    width: 478, 
    title: 'My Panel', 

    initComponent: function() { 
     var me = this; 

     Ext.applyIf(me, { 
      items: [ 
       { 
        xtype: 'button', 
        text: 'Button1' 
       }, 
       { 
        xtype: 'button', 
        text: 'Button2', 
        listeners: { 
         click: { 
          fn: me.onButtonClick, 
          scope: me 
         } 
        } 
       }, 
       { 
        xtype: 'button', 
        text: 'Button3' 
       }, 
       { 
        xtype: 'gridpanel', 
        id: 'myGrid', 
        title: 'My Grid Panel', 
        store: 'Button1Store', 
        columns: [ 
         { 
          xtype: 'gridcolumn', 
          dataIndex: 'Name', 
          text: 'Name' 
         }, 
         { 
          xtype: 'gridcolumn', 
          dataIndex: 'Email', 
          text: 'Email' 
         } 
        ] 
       } 
      ] 
     }); 

     me.callParent(arguments); 
    }, 

    onButtonClick: function(button, e, eOpts) { 
     //alert("button 2"); 

     var gridVar = Ext.getCmp('myGrid'); 
     var newStore = Ext.getCmp('MyStore2'); 
     //alert(gridVar.columns[0].dataIndex); 
     //gridVar.bindStore(newStore); 
     gridVar.reconfigure(newStore, gridVar.columns); 
    } 

}); 

商店1和模式1:

Ext.define('MyApp.store.Button1Store', { 
    extend: 'Ext.data.Store', 

    requires: [ 
     'MyApp.model.Button1Model' 
    ], 

    constructor: function(cfg) { 
     var me = this; 
     cfg = cfg || {}; 
     me.callParent([Ext.apply({ 
      autoLoad: true, 
      model: 'MyApp.model.Button1Model', 
      storeId: 'MyStore1', 
      data: [ 
       { 
        Name: '1', 
        Email: 'test1' 
       }, 
       { 
        Name: '2', 
        Email: 'test2' 
       }, 

      ] 
     }, cfg)]); 
    } 
}); 

Ext.define('MyApp.model.Button1Model', { 
    extend: 'Ext.data.Model', 

    idProperty: 'Button1Model', 

    fields: [ 
     { 
      name: 'Name' 
     }, 
     { 
      name: 'Email' 
     } 
    ] 
}); 

商店2和模式2:

Ext.define('MyApp.store.Button2Store', { 
    extend: 'Ext.data.Store', 

    requires: [ 
     'MyApp.model.Button2Model' 
    ], 

    constructor: function(cfg) { 
     var me = this; 
     cfg = cfg || {}; 
     me.callParent([Ext.apply({ 
      model: 'MyApp.model.Button2Model', 
      storeId: 'MyStore2', 
      data: [ 
       { 
        Name: '3', 
        Email: 'test3' 
       }, 
       { 
        Name: '4', 
        Email: 'test4' 
       } 
      ] 
     }, cfg)]); 
    } 
}); 

Ext.define('MyApp.model.Button2Model', { 
    extend: 'Ext.data.Model', 

    fields: [ 
     { 
      name: 'Name' 
     }, 
     { 
      name: 'Email' 
     } 
    ] 
}); 

當我嘗試這樣,網格中的標題和數據就消失了。我嘗試了gridVar.bindStore(newStore);,但它引發了一個錯誤,指出數據爲空或未定義。請幫助...

+0

getCmp爲您返回一個組件。不知道你的意思是不知道如何使用重新配置。只需傳遞一個新的商店實例,建議您閱讀文檔,這裏有很多示例。 – 2013-03-20 12:28:05

+0

嗨..我已經嘗試過'Ext.data.StoreManager.lookup()'以及..但是相同的結果.. – CARTIC 2013-03-20 13:01:17

回答

0

如果您對所有操作使用相同的網格,則更改商店的各種操作的代理URL並使用load()方法將數據填充到網格!

+0

嗨..謝謝了很多的提示,它的工作正常。我使用的按鈕onClick中的代碼是'var gridVar = Ext.getCmp('myGrid')。getView(); var store = gridVar.getStore(); store.getProxy()。url ='data/Button2Data.json'; gridVar.store.load();'。我也需要一些說明。在這裏,我有相同的列和數據索引。但是,如果我必須顯示不同數量的列,這個解決方案是否也可以幫助您... – CARTIC 2013-03-21 06:09:40

+0

使用load()時使用回調函數,以便您可以處理成功和失敗。在成功之後,您可以編寫自定義邏輯。你能否詳細說明你正在試圖用coulmns做各種各樣的行動 – aswininayak 2013-03-22 13:51:45