Custom onClick Javascript function with toggleSelection
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?
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);
}
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.