Possible Bug: Filtered Table uses the filtered index value, not the table data index value

Imagine a table with 100 rows. Any selected row (`table.selectedRow.index) will have an index from 0 - 99.
Now filter that table, let's assume that there are 2 rows showing. When you select these rows the available indexes are 2, 0-1.

Grabbing the data from the table, when filtered (table.data) retrieves all 100 rows.

This pretty much makes it impossible to update table which has been filtered.

In my scenario, which I have seen others express here, you perform and action on button click of a row. Perform some logic or other goo, then update the row to reflect the changes. For clarity, updating by grabbing the table data, updating the indexed value of that array (table data) and putting it back into the table. E.g.:

const tableData = table.data;
const row = table.selectedRow.data;
const index= table.selectedRow.index;
const newDataRow = someSuperDuperFunction(row);
tableData[index] = newDataRow;
table.setData(tableData);

there needs to be some consistency with filtered tables where the table data and selected index values are from the same dataset, either the filtered set or the entire data set.

I hope this makes sense. As things sit now there is no way to programmatically update the data in a table which is filtered.

1 Like

Hey @brettski :eyes:

Our engineers are currently looking into how to make row selection work better with indexes. I'll pass it along here when there's an update! Thanks for surfacing this.

In the meantime, my recommendation would be to have an index in the table data itself, if your data doesn't already have a primary key you can pass your table something like:

data.map((row, i) => ({...row, tableIndex: i}));

In which case your code becomes:

const tableData = table.data;
const row = table.selectedRow.data;
const index = tableData.findIndex(d => d.tableIndex === row.tableIndex);
const newDataRow = someSuperDuperFunction(row);
tableData[index] = newDataRow;
table.setData(tableData);

Definitely not the greatest, but could that work?

1 Like

Yeah, that is a doable work-around (there are unique values in the data). Nice thought. Thank you!

Just want to leave a note here that you can also try using the displayedDataIndices property on your table to correlate the index a row has when the table has been filtered with the index it has when the table is not filtered!

1 Like