frappe/datatable

What is the best way to get data from the selected rows?

wgwz opened this issue · 3 comments

wgwz commented

I'm guessing I can probably make use of this:

export default function filterRows(rows, filters) {
let filteredRowIndices = [];
if (Object.keys(filters).length === 0) {
return rows.map(row => row.meta.rowIndex);
}
for (let colIndex in filters) {
const keyword = filters[colIndex];
const filteredRows = filteredRowIndices.length ?
filteredRowIndices.map(i => rows[i]) :
rows;
const cells = filteredRows.map(row => row[colIndex]);
let filter = guessFilter(keyword);
let filterMethod = getFilterMethod(filter);
if (filterMethod) {
filteredRowIndices = filterMethod(filter.text, cells);
} else {
filteredRowIndices = cells.map(cell => cell.rowIndex);
}
}
return filteredRowIndices;
};

But it's unclear how I could get the rows that are selected, idiomatically at least, using this library.

It might be a useful addition to the API to add a getSelectedRows function following this one: https://github.com/frappe/datatable/blob/master/src/datatable.js#L174-L176

Any suggestions appreciated.

wgwz commented

Bingo,

getCheckedRows() {
if (!this.checkMap) {
return [];
}
let out = [];
for (let rowIndex in this.checkMap) {
const checked = this.checkMap[rowIndex];
if (checked === 1) {
out.push(rowIndex);
}
}
return out;
}

I'd suggest documentation on this. I will try to submit an MR for this.

wgwz commented

Also it's still pretty unclear, at least to me how to use the filterRows function. Again, once I get examples I'd be glad to contribute. Although it doesn't look like the docs are in this repo.

wgwz commented

I had a situation where I wanted to get the selected data out an initial datatable object and move it to a new datatable. Getting that data out of the datatable object feels a little clunky, at least the way I did it:

function getSelectedRows(datatable) {                                                                                                                                                                                                         
  console.log("getSelectedRows")                                                                                                                                                                                                              
  console.log(datatable.options.data);                                                                                                                                                                                                        
  console.log(datatable.rowmanager.getCheckedRows());                                                                                                                                                                                         
  var selectData = datatable.rowmanager.getCheckedRows().map(x => datatable.options.data[parseInt(x)]);                                                                                                                                       
  console.log(selectData);                                                                                                                                                                                                                    
  return selectData                                                                                                                                                                                                                           
}

Any suggestions appreciated, in terms of a better way to do this.