Monday, January 12, 2009

ExtJS Tip : Select the first row when grid is loaded

Let's say that you have a grid called "mygrid" and you would like the first row of that grid to be selected when the grid data is loaded. The following snippet of code should work ...

Ext.getCmp('mygrid').store.on('load', function(){
var grid = Ext.getCmp('mygrid');
grid.getSelectionModel().selectRow(0);
grid.fireEvent('rowclick', grid, 0)
}, this, {
single: true
});

6 comments:

  1. Thanks for ur post. It helped me a lot!!!

    ReplyDelete
  2. A slightly better option could be to use the Grid View helpers instead of firing an event:

    Ext.getCmp('mygrid').on('viewready', function(){
    this.getSelectionModel().selectFirstRow();
    this.getView().focusRow(0);
    }, this, { single:true });

    ReplyDelete
  3. both don't work in my case -- the event itself is not fired .

    ReplyDelete
  4. AD: It is worked but below modification has done in my code
    .....{
    xtype:'grid',
    sm:sm,
    cm:cm,
    store:store,
    listeners:{
    'viewready':function(grid){
    grid.getSelectedModel().selectFirstRow();
    grid.fireEvent('rowClick', grid, 0);
    }
    }
    }

    ReplyDelete
  5. is this code also valid for extjs 4.0, actually i tried this, but its not working

    ReplyDelete