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

onClick: combine expand and JS function #298

Open
rickhelmus opened this issue Dec 8, 2022 · 3 comments
Open

onClick: combine expand and JS function #298

rickhelmus opened this issue Dec 8, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@rickhelmus
Copy link

Hello,

Currently the onClick function argument to reactable() only seems to accept either expand, select or a JS function. I would combine group expanding and execution some JS whenever a group is expanded. From what I can tell the JS function to expand a single group is currently not exported, so I was wondering if there is a workaround?

Thanks,
Rick

@glin
Copy link
Owner

glin commented Dec 10, 2022

Hi, interesting question, and my first thought would be to add a way to expand individual rows through the JavaScript API so you can use a custom click action to expand and do whatever you want. However, there are some issues with publicizing the API for expanding individual rows that I need to figure out first, and although technically possible today, it would likely break by the next release.

A second thought is that there's also an undocumented, internal row.toggleRowExpanded() method on the rowInfo object that the custom click action receives. Although undocumented, it should be fairly stable and I don't see why we couldn't document that publicly. Here's a quick example which should be 99% safe to use, but still not 100%, so definitely use at your own risk for now :)

library(reactable)
library(htmltools)

data <- MASS::Cars93[, c("Manufacturer", "Model", "Type", "Price")]

reactable(
  data,
  groupBy = "Manufacturer",
  # NOTE: this is an undocumented workaround to toggle expanded rows on click in
  # a custom cell click action.
  onClick = JS("row => {
    row.toggleRowExpanded()
    console.log('do something else here with grouped row subrows:', row.subRows)
  }")
)

@glin glin added the enhancement New feature or request label Dec 10, 2022
@rickhelmus
Copy link
Author

Hello,

Many thanks, the internal function seems to work well. Is there a particular reason in mind why this function is 1% unsafe? :-)

@jhk0530
Copy link

jhk0530 commented Nov 7, 2023

Hello, If you need combine select and custom JS function like me,
There's another undocumented API for select: use row.toggleRowSelected().

library(reactable)
library(htmltools)

data <- MASS::Cars93[, c("Manufacturer", "Model", "Type", "Price")]

reactable(
  data,
  groupBy = "Manufacturer",
  theme = reactableTheme(
    rowSelectedStyle = list(backgroundColor = "#eee", boxShadow = "inset 2px 0 0 0 #ffa62d")
  ),
  onClick = JS("row => {
    row.toggleRowSelected()
    console.log('do something else here with grouped row subrows:', row.subRows)
  }")
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants