PDA

View Full Version : How to get all selected row id in ext js grid?



jocatastar
1 Dec 2013, 8:07 PM
I am using Ext.grid.panel and with that i am using check box model as a selection type.

If i select one row or greater than one row i want to fire a event and in that event i want to get all the selected rows.

Farish
2 Dec 2013, 12:06 AM
its very simple. all you need is to add a listener for the selectionchange event of the grid:


listeners:
{
selectionchange: function(sModel, sRecords)
{
console.log(sRecords); // sRecords will be an array of selected record(s) or null in case there is no selection. you can loop through it to get the id of each record.
}
}

jocatastar
2 Dec 2013, 3:59 AM
Thank you for the reply farish,
i did it this way


select:function(dv, record, index, eOpts ){
var grid = Ext.getCmp('gridid');
var selection= grid.getSelectionModel();
items=[];
for(var i=0;i < grid.store.getCount();i++){
if(selection.isSelected(i)){
items.push(grid.store.getAt(i).data.index);
}
}
}

Farish
2 Dec 2013, 4:01 AM
instead of looping through all store records, a better way to do it is:


var sRecords = grid.getSelectionModel().getSelection();

this will directly give you the selected records.

Farish
2 Dec 2013, 4:15 AM
one more thing, select only fires when a record is selected and not when one is deselected. So it might not work in every case of the user deselects some record(s). I would still recommend using selectionchange.

jocatastar
2 Dec 2013, 4:23 AM
Yes farish you are right, it will not fire on unselected.

=D>Good catch. I will change to selectionchange

And if i do like this

var sRecords = grid.getSelectionModel().getSelection();

how can i insert particular value into an array, which i am doing

if(selection.isSelected(i)){ items.push(grid.store.getAt(i).data.index); }

Farish
2 Dec 2013, 4:27 AM
assuming that you have the selected records array in sRecords variable:


for(var i=0; i<sRecords.length; i++)
items.push(sRecords[i].get('index'));

or you can use .data.index instead of get method.

jocatastar
2 Dec 2013, 4:28 AM
Nice !!!thank you very much