Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom onClick Javascript function with toggleSelection #394

Open
Odraio opened this issue Sep 30, 2024 · 2 comments
Open

Custom onClick Javascript function with toggleSelection #394

Odraio opened this issue Sep 30, 2024 · 2 comments
Labels
doc Documentation issue or suggestion

Comments

@Odraio
Copy link

Odraio commented Sep 30, 2024

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?

@anisriaz
Copy link

anisriaz commented Oct 1, 2024

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);

}

@glin
Copy link
Owner

glin commented Oct 7, 2024

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 glin added the doc Documentation issue or suggestion label Oct 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Documentation issue or suggestion
Projects
None yet
Development

No branches or pull requests

3 participants