1
votes

I've been having issues with the dgrid selection mix-in with multi selects.

Using the selection property (for example)

var selected = Object.keys(datatable.selection)

it returns an array of row ids as expected. However the ORDER of those ids seems to be "arbitrary". It seems perhaps that the order of selecting has an affect.

In any event, in the datatable, I want the selected rows to be returned in order that they display in the list, and they do not.

I can get them in the proper order using dojo.query(".dgrid-selected", datatable.domNode), and use the HTML element to get the row data, but this seems like a hack.

I cannot find a proper method to do this on the SitePen docs. Anyone?

1

1 Answers

1
votes

I don't think that there is a direct way to do that. The Object.keys(datatable.selection) returns the array of ids in the order in which the rows are selected. You can use some built-in functions of d-grid and JS to achieve this. Below are the steps:

  1. Get the id by Object.keys.

    var selected = Object.keys(datatable.selection)

  2. Create a list of objects comprising of id and rowIndex of element

Code:

var dataList= [];
for(var i=0; i< selected.length; i++){
    dataList.push({id: selected[i], index: datatable.row(selected[i]).element.rowIndex});
}
  1. Sort the list using index as the attribute:

dataList.sort(function(a, b){ return a.index- b.index; })

The resulting dataList would have the list of objects in order in which they appear in the grid.