Dynamically unselect all table rows

I see there's a way to script selecting all the rows in a table (table1.selectRow(_.range(0, table1.data.length))), but I'm struggling to find a way to subsequently allow the user to deselect all rows. Is this doable?

Hey there @laurab! You can run table1.selectRow(null) and that should work!

2 Likes

@justin while selectRow(null) works great for unselecting all, is there a way to unselect a specific row while maintaining rest of the selection?

Hey @sufi!

You can do it by filtering the index you want to deselect out of table1.selectedRow.index and then passing the result back to .selectRow, since that will set the selected row indices to be all the currently selected ones minus the deselected one!

Does something like the following work for you?

table2.selectRow(table2.selectedRow.index.filter(index => index !== indexToDeselect));

You'll need to replace indexToDeselect with i or however else you're getting the index of the deselected row.

If you don't want to do it by index you could create a conditional for the row you want to deselect with something like:

const someCondition = row => row.sales > 300;
const currentSelections = table2.selectedRow.index;
const newSelections = currentSelections.filter((_, i) => someCondition(table2.selectedRow.data[i]));
table2.selectRow(newSelections);