reactable icon indicating copy to clipboard operation
reactable copied to clipboard

Custom onClick Javascript function with toggleSelection

Open Odraio opened this issue 1 year ago • 2 comments

Using a custom onClick Javascript function does not seem to work anymore when using toggleSelection to select a single row (by index):

Code snippet

function setSingleSelectedRow(tableName, rowInfo, colInfo, state, instance) {     
     state.toggleSelection(rowInfo.index)
}

The code did work with version 0.2.3, but it seems to be broken with version 0.4.4 (our project has been updated recently). Could you provide an alternative solution?

Odraio avatar Sep 30 '24 14:09 Odraio

Alternative Solution to Select a Single Row

function setSingleSelectedRow(tableName, rowInfo, colInfo, state, instance) { // Clear the current selection to ensure only one row is selected state.resetRowSelection();

// Select the new row by index
state.toggleRowSelected(rowInfo.index, true);

}

anisriaz avatar Oct 01 '24 08:10 anisriaz

I think state.toggleSelection was removed during an internal library upgrade at some point, but it was also never documented nor officially supported.

It was changed to row.toggleRowSelected(), so usage would now be:

library(reactable)

reactable(
  MASS::Cars93, 
  selection = "single", 
  onClick = JS("function(row, column, rowInfo) {
    console.log('selected row', row.index)
    row.toggleRowSelected()
  }")
)

row.toggleRowSelected() is also not documented, but it should be and I think it's probably safe enough to use for now.

glin avatar Oct 07 '24 03:10 glin