1

SlickGrid:

Is there a 'selected'/'isSelected' or similar property for grid rows?

If I use 'grid.getSelectedRows' then the rows seem to be returned in the order they were selected (clicked), not necessarily in the order they appear on the grid. I want the selected rows in visible order, top to bottom.

I can get each item with grid.getDataItem but how do I determine whether the row is selected? Do I need the properties for the row rather than the data item?

for (var i = 0, ii = grid.getDataLength(); i < ii; i++) {
var item = grid.getDataItem(i);
if (!item.selected()) {          <- fails because there is no selected property
        continue;
}...

Thanks!

1 Answers1

1

I believe this answer would be what you're looking for, according to the docs found here. Max Brodin is correct in suggesting grid.getSelectedRows().

EDIT: To accommodate @AnotherFineMess's request to have it sorted top-bottom in the grid, the code should be:

var items = grid.getSelectedRows().sort(function(a,b){return a - b});
Community
  • 1
  • 1
william.taylor.09
  • 2,145
  • 10
  • 17
  • The problem is that the list of rows returned by grid.getSelectedRows() is ordered according to the order in which they were selected. I want a list of selected rows ordered according to the screen view, top to bottom, regardless of the order in which the rows were selected. – AnotherFineMess Apr 06 '15 at 15:38
  • Why not just sort it by using grid.getSelectedRows().sort();? – william.taylor.09 Apr 06 '15 at 15:40
  • The grid does have sort options, yes. Unfortunately users will not always start at the top and click the rows they need in order. They peck and select all over the place and then I need to maintain the sort order in the selected list, top to bottom. – AnotherFineMess Apr 06 '15 at 15:42
  • The getSelectedRows() function will give you an array of indexes that have been clicked. These indexes represent the rows in the grid. The sort() method I tacked onto the end of the getSelectedRows() function will sort those indexes in order, from top to bottom. – william.taylor.09 Apr 06 '15 at 15:44
  • Thanks, it's almost there. If I select items with id [10, 9, 8] then it sorts to [10, 8, 9] instead of [8, 9 , 10] so I suppose it is sorting the ids as string rather than integer? – AnotherFineMess Apr 06 '15 at 16:02
  • Can you print out + comment what grid.getSelectedRows() is returning to you? – william.taylor.09 Apr 06 '15 at 16:04
  • @AnotherFineMess, I just realized that it's sorting lexicographically. What you really want is: grid.getSelectedRows().sort(function(a,b){return a - b}); – william.taylor.09 Apr 06 '15 at 16:12