7

I have a slickgrid in which some rows are hidden by a filter (DataView).

When I now call the getSelectedRows method of the grid I get the indices of the visibly selected rows. But I need the actual data of the selected rows.

Preli
  • 2,953
  • 10
  • 37
  • 50

5 Answers5

21

You must do something like this:

var selectedData = [],
    selectedIndexes;

selectedIndexes = _grid.getSelectedRows();
jQuery.each(selectedIndexes, function (index, value) {
  selectedData.push(_grid.getData()[value]);
});

Right now the selectedData variable contains data for selected rows.

matma
  • 1,104
  • 1
  • 10
  • 17
  • 1
    Ahhh, grid.getData, that was the method I was looking for. BUT grid.getData[value] did not work I had to use grid.getData().getItem(value). – Preli Oct 30 '11 at 11:05
  • My apologize, I use a Andrew Child's fork (https://github.com/andrewchilds/SlickGrid) and this code works well on it. – matma Oct 30 '11 at 16:56
  • I think the difference between `grid.getData()[value]` and `grid.getData().getItem(value)` relates to the type used for storage in the grid: the former works for arrays, the latter for Slick.DataView. I mean, I'm guessing; I could be totally wrong, but try it out. – Jonas Kölker Dec 03 '12 at 13:50
4

You have a mistake. It needs to be "getDataItem" and not "getData".

var selectedData = [],enter code here`selectedIndexes;

selectedIndexes = _grid.getSelectedRows();
jQuery.each(selectedIndexes, function (index, value) {
    selectedData.push(_grid.getDataItem(value));
});
Leigh
  • 28,765
  • 10
  • 55
  • 103
eliprodigy
  • 600
  • 6
  • 8
  • The solution @matma provided works fine if you're not using a DataView. It's not really a mistake. Your solution is probably better though as it will always work. – idbehold May 20 '13 at 19:50
2
hObjMarcado  = ( grid.getSelectedRows());
for( var a_id in hObjMarcado ) {
    vres.push( dataview.getItem( hObjMarcado[a_id] ));
    //la opcion getItem obtiene el elemento especifico,
    //aun con filtro.
}
return vres;
Aubin
  • 14,617
  • 9
  • 61
  • 84
Emma Gasca
  • 21
  • 1
1

You can also use this line in the .each loop to pull the data from the dataView instead of using getData() from the grid object, since that seems to be inconsistent depending on the fork:

var selectedData = [],
    selectedIndexes;

selectedIndexes = _grid.getSelectedRows();
jQuery.each(selectedIndexes, function (index, value) {
    selectedData.push(_dataView.getItemById(value));
});
Axle
  • 1,807
  • 2
  • 14
  • 18
  • 2
    Actually, I believe you'd want to use getItemByIdx, not getItemById. getItemByIdx will get the data by index, while getItemById gets the data by the unique id you gave it when creating your data. They would do the same thing if the id you specified for each item is equal to its index in the grid. However, in my case, they didn't match. Thanks for the code though. – dallin Sep 25 '12 at 00:26
0

If you access grid from other control like . click button

var selectRow = gridInstance.getSelectedRows();
alert(gridInstance.getDataItem(selectRow).columnName)
Wolf
  • 6,361
  • 2
  • 28
  • 25