How to select multiple rows by using grid.getSelectionModel().select(Indexes) in extjs 4.1

8.5k views Asked by At

I want to select multiple rows by using grid.getSelectionModel().select(Indexes) in extjs 4.1 Anyone knows how to do it?? Here is my code:

var grid = Ext.getCmp('GridStudents');
var fieldvalues = '2054,2055,2057';
var arr = fieldvalues.split(',');

for (var j = 0; j < arr.length; j++) 
{
  index = grid.store.find('StudentID', arr[j]);
  grid.getSelectionModel().select(j);
}
1

There are 1 answers

6
Krzysztof On BEST ANSWER

First of all your selection model must have mode MULTI or SIMPLE.

Then you can use method selectRange(startRow, endRow) when you want to select bunch of records which are in one block.

You can also use select and pass array of records or select one by one using index.

Both this function accepts another parameter keepExisting. When set to true, existing selection is kept (as name suggests).

Also you pass j to select method instead of index.

So easiest fix would be:

for (var j = 0; j < arr.length; j++) 
{
    var index = grid.store.find('StudentID', arr[j]);
    grid.getSelectionModel().select(index, true);
}

If your model is configured for multiple selection, it should work.

Fiddle: http://jsfiddle.net/7ofLb3Ls/3/

As an alternative you can try this code:

var grid = sender.up('grid');
var fieldvalues = '2054,2055,2057';
var arr = fieldvalues.split(',');
var records = Ext.Array.filter(
    grid.store.data.items,
    function(r) {
        return arr.indexOf(''+r.get('StudentID')) !== -1;
     }
 );
 grid.getSelectionModel().select(records);

Fiddle: http://jsfiddle.net/7ofLb3Ls/4/