2012-05-04 64 views
0

我在ExtJs 3.3.1中使用了一個livegrid,但相信這個問題對於ExtJs是全球性的。 商店的聽衆如何知道事件來自哪個網格?ExtJs:確定引發商店更新事件的網格

這裏爲什麼和一些代碼。

我有一個商店的偵聽器和更新,我想知道在網格中選擇了哪些行,並暫停事件。這一切,以便我可以在網格中進行選擇,更新該範圍中的字段並在整個選擇中更新該字段。選擇沒有複選框,只需突出顯示行。由於該監聽器被許多網格,我需要一種方式來獲得它froml什麼gridlistener得到的參數,但是即

Ext.override(Ext.ux.grid.livegrid.Store, { 
    listeners: { 
     'update': function(store, record, action) { 
     if (action=='commit'){ //each update has 2 actions, an edit and a commit 
      var selected = grid.getSelectionModel().getSelections(); //need to know which grid 
      if (selected.length>1){ //if more than one row selected 
      grid.suspendEvents(); 
      store.writer.autoSave = false; 
      for(var i=0; i < selected.length; i++){ 
       if (this.fieldChanged) { 
       for (var name in this.fieldChanged) { 
        //get the field changed and update the selection with the value 
        if (selected[i].get(name)!=this.fieldChanged[name]){ 
        selected[i].set(name, this.fieldChanged[name]); 
        } 
       } 
       } 
      } 
      grid.resumeEvents(); 
      store.fireEvent("datachanged", store); 
      store.writer.autoSave = true; 
      } 
     } 
     if (action=='edit'){ 
      this.fieldChanged = record.getChanges() 
     } 
     } 
    } 
    }); 
+0

你究竟在做什麼?網格應該自動更新它的東西。從您的問題不清楚什麼_real_問題是 – sha

+0

問題是,我使用這個監聽器的許多網格,我需要知道在監聽器本身它正在處理的網格 – peter

+0

我明白這一點。你在聽者中究竟想做什麼?看起來你正在手動更新網格,並且應該自動發生 – sha

回答

1

這將是一個擴展更容易,但它可以在一個覆蓋進行爲好。

MyGridPanel = Ext.extend(Ext.ux.grid.livegrid.EditorGridPanel, { 
    initComponent: function(){ 
     MyGridPanel.superclass.initComponent.call(this); 
     this.store.grid = this; 
    } 
}); 

編輯---顯示如何做到這一點在覆蓋,這是不漂亮,但它是有用的。

var oldInit = Ext.ux.grid.livegrid.EditorGridPanel.prototype.initComponent; 
Ext.override(Ext.ux.grid.livegrid.EditorGridPanel, { 
    initComponent: function(){ 
     oldInit.call(this); 
     this.store.grid = this; 
    } 
}); 
+0

但是,那麼我將不得不在我的所有網格上這樣做? – peter

+0

@peter我更新了我的回覆,將其顯示爲覆蓋。 – pllee

0

找到了解決辦法我只能存儲,記錄和行動,我重寫livegrid包括一個提到自己在其商店像這樣

Ext.override(Ext.ux.grid.livegrid.EditorGridPanel, { 
    listeners: { 
     'afterrender': function(self) { 
     this.store.grid = this.id; 
     } 
    } 
    }); 

然後在我的店裏聽衆可以參考我store.grid

+0

直接在原型中添加偵聽器是一種不好的做法。即:var grid = new Ext.ux.grid.livegrid.EditorGridPanel({listeners:{'afteredit':function(){}})會清除afterrender偵聽器。 – pllee

1

可能會有更多的網格使用商店。最好在Ext Js 4中,你可以看到Gridpanel類如下:

//Associate all rendered grids to the store, so that we know which grids use a store. 
Ext.util.Observable.observe(Ext.grid.Panel); 
Ext.grid.Panel.on('render', function(grid){ 
    if (!grid.store.associatedGrids){ 
     grid.store.associatedGrids=[]; 
    } 
    grid.store.associatedGrids.push(grid); 
});