-
Notifications
You must be signed in to change notification settings - Fork 80
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
Hyperlink Onclick Not Rendering Inside Reactable Table #150
Comments
Hi, you've stumbled on a quirk with how reactable uses React to render HTML tags, sorry. In React, the For workarounds, I can think of 3 options:
Here's an example that shows each option, and thanks for reporting this. library(reactable)
library(htmltools)
reactable(
MASS::Cars93[, 1:5],
columns = list(
# Using React to handle click events (onClick function)
# Note subtle differences and caveats in https://reactjs.org/docs/handling-events.html
Manufacturer = colDef(
cell = function(value, index) {
id <- paste0("Manufacturer-", index)
onClick <- JS(sprintf("function(e) { console.log('clicked', '%s', e.target.id) }", id))
tags$button(id = id, onClick = onClick, value)
}
),
# Using native DOM click events (onclick attribute)
Model = colDef(
cell = function(value, index) {
id <- paste0("Model-", index)
onclick <- sprintf("console.log('clicked', '%s', this.id)", id)
sprintf('<button id="%s" onclick="%s">%s</button>', id, onclick, value)
},
html = TRUE
),
# Using reactable's cell click actions (onClick function below)
Type = colDef(
cell = function(value, index) {
tags$button(value)
}
)
),
onClick = JS("function(rowInfo, colInfo) {
if (colInfo.id !== 'Type') {
return
}
const id = colInfo.id + '-' + (rowInfo.index + 1)
console.log('clicked', id)
}")
) (Also as a side note, I recommend using buttons instead of links for click actions, even if they have to be styled as links - https://developer.mozilla.org/en-US/docs/Learn/Accessibility/HTML#onclick_events) |
Here's an example of rendering buttons with library(reactable)
library(htmltools)
reactable(
MASS::Cars93[, 1:5],
columns = list(
Model = colDef(
cell = function(value, index) {
onclick <- sprintf("console.log('clicked row %s, element', this, event.target)", index)
tags$button(onclick = onclick, value)
}
)
)
) |
I'm trying to create a column in a reactable table (in Shiny) that has hyperlinks, when the hyperlinks are clicked the tag id is assigned to an input variable. My code is below.
When the table is rendered the hyperlinks are successfully created but the onclick event seems to be dropped.
If I generate the link outside the reactable table the onclick function is included so this leads me to believe there is something that reactable doesn't like about this?
Any ideas?
The text was updated successfully, but these errors were encountered: