peter
peter

Reputation: 42182

ExtJs: determine grid that fires the update event on a store

i use a livegrid in ExtJs 3.3.1 but believe this question is global to ExtJs. How does a listener on a store know which grid the event comes from ?

Here why and some code.

I have a listener on a store and on update i would like to know which rows were selected in the grid and also suspend the events. This all so that i can make a selection in the grid, update a field in that range and update that field in the whole selection. Selection is done without a checkbox, just by highlighting the rows. Since this listener is used by many grids i need a way to get it froml what the gridlistener gets as parameters but that is only store, record and action

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()
        }
      }
    }
  });

Upvotes: 1

Views: 3079

Answers (3)

Christiaan Westerbeek
Christiaan Westerbeek

Reputation: 11137

There may be more grids using the store. Preferably in Ext Js 4 you observe the Gridpanel class like so:

//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);
});

Upvotes: 1

pllee
pllee

Reputation: 3959

It would be easier in an extension but it can be done in an override as well.

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

edit --- Showing how to do it in an override, it isn't pretty but it is useful.

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;
    }
});

Upvotes: 1

peter
peter

Reputation: 42182

Found a solution myself, i override the livegrid to include a reference to itself in its store like so

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

Then in my store listener i can refer to store.grid

Upvotes: 0

Related Questions