Monday, October 22, 2012

ExtJS Tweak : Selecting first and last row of a grid

Very often we need to select a row by default in the grid, since some other pieces of code are linked to selection of the rows in grid , for example like charts rendering data in the grid and something like that.

The problem is the the first row in grid is not selected by default in ExtJS and we have to break our heads.
The event - 'viewready' is the answer. In the documentation , it is explicitly written to use this event to select a row in grid by default.

this small snippet of code will do the job(select the first row),
suppose you have defined your grid as 'grid', then:

 grid.on('viewready',function(){  
           this.getSelectionModel().select(0);  
      });  
Now we would also want some times to select the last row in case we have added a record. The following snippet will do the job beautifully.

 xmlgrid.getSelectionModel().select(store.getCount()-1);  
We have xmlgrid as the grid and "store" is name of data store attached to the grid.

No comments:

Post a Comment