Carlo
Carlo

Reputation: 41

extjs 4 grid fireevent itemclick

How do you make a fireEvent itemclick after the store loads.

I have this but it doesn't work:

pcfstore.on('load', function(){
   //auto select first row;
   Ext.getCmp('pcf_grid').getSelectionModel().select(0); // this works

   //fire itemclick event
  var grid= Ext.getCmp('pcf_grid');
  grid.fireEvent('itemclick', grid, 0); //this doesnt work

}); 

Here is my itemclick event in grid view:

viewConfig: {
    listeners: {
    itemclick: function(dv, record, item, index, e) {
           alert(record.data.code);
       }
    }
}

Basically when the grid loads, it should fire the alert window of the selected first row of the grid.

Upvotes: 4

Views: 27685

Answers (2)

JLavoie
JLavoie

Reputation: 17626

After hours of search, I found a solution. It looks like there is an issue with ExtJs4 that make the following functions impossible to work for me:

grid.getSelectionModel().select(0);

or

grid.getView().select(0); // note that this function is deprecated in ExtJS4!!

In my controller, I use this code instead:

store.load({
    callback: function (records, operation, success) {        
        var rowIndex = this.find('id', myRecord);  //where 'id': the id field of your model. You can replace 'id' with your unique field.. And 'this' is your store.
        grid.getView().select(rowIndex);
    }
})

Where myRecord is the record to highlight and select. It then worked like a charm. I got the row 0 highlighted and selected. However, the itemclick listeners were not fired when the row is select with this code.

Upvotes: 0

Molecular Man
Molecular Man

Reputation: 22386

itemclick is event of View but not of Grid. Try to use:

grid.getview().fireEvent('itemclick', grid, 0);

And by the way why not use selectionchange instead.

UPDATE

If you have both itemcontextmenu and selectionchange handlers it can be a little bit confusing. In this case I recommend back to square one and use itemclick event.

But your code need to have some modifications:

  1. Assign itemclick event to grid, NOT to it's view.
  2. When firing itemclick pass actual record, NOT an index

like this:

grid.getSelectionModel().select(0);
grid.fireEvent('itemclick', grid, grid.getSelectionModel().getLastSelected());

And here is fiddle to demonstrate what I'm talking about.

Upvotes: 10

Related Questions