@@ -1273,11 +1273,12 @@ reactable(iris[1:5, ], rownames = TRUE)
1273
1273
1274
1274
1275
1275
## Cell Click Actions
1276
- You can add cell click actions using ` onClick ` , which accepts the following values:
1276
+ You can add cell click actions using the ` onClick ` argument, which accepts the
1277
+ following values:
1277
1278
1278
1279
- ` "expand" ` to expand the row
1279
1280
- ` "select" ` to select the row
1280
- - a JavaScript function for a custom action, e.g., sending the click event to Shiny
1281
+ - A JavaScript function for a custom action, e.g., sending the click event to Shiny
1281
1282
1282
1283
### Expand on click
1283
1284
``` {r}
@@ -1297,20 +1298,51 @@ reactable(iris[1:5, ], selection = "multiple", onClick = "select")
1297
1298
```
1298
1299
1299
1300
### Custom action
1301
+
1302
+ This example uses a custom click action to create custom "show details" action
1303
+ buttons in each row of the table:
1304
+
1300
1305
``` {r}
1301
- reactable(iris[1:5, ], onClick = JS("
1302
- function(rowInfo, colInfo, state) {
1303
- window.alert('clicked row ' + rowInfo.index + ', column ' + colInfo.id + ', on page ' + state.page)
1306
+ data <- cbind(
1307
+ MASS::Cars93[1:5, c("Manufacturer", "Model", "Type", "Price")],
1308
+ details = NA
1309
+ )
1304
1310
1305
- // Send the click event to Shiny, which will be available at input$clicked
1306
- // (Note that the row index starts at 0 in JavaScript, so we add 1)
1311
+ reactable(
1312
+ data,
1313
+ columns = list(
1314
+ # Render a "show details" button in the last column of the table.
1315
+ # This button won't do anything by itself, but will trigger the custom
1316
+ # click action on the column.
1317
+ details = colDef(
1318
+ name = "",
1319
+ sortable = FALSE,
1320
+ cell = function() htmltools::tags$button("Show details")
1321
+ )
1322
+ ),
1323
+ onClick = JS("function(rowInfo, colInfo) {
1324
+ // Only handle click events on the 'details' column
1325
+ if (colInfo.id !== 'details') {
1326
+ return
1327
+ }
1328
+
1329
+ // Display an alert dialog with details for the row
1330
+ window.alert('Details for row ' + rowInfo.index + ':\\n' + JSON.stringify(rowInfo.row, null, 2))
1331
+
1332
+ // Send the click event to Shiny, which will be available in input$show_details
1333
+ // Note that the row index starts at 0 in JavaScript, so we add 1
1307
1334
if (window.Shiny) {
1308
- Shiny.onInputChange('clicked ', { column: colInfo.id, index: rowInfo.index + 1 })
1335
+ Shiny.setInputValue('show_details ', { index: rowInfo.index + 1 })
1309
1336
}
1310
- }
1311
- ") )
1337
+ }")
1338
+ )
1312
1339
```
1313
1340
1341
+ > ** WARNING:** Custom click actions are currently not accessible to keyboard
1342
+ > users, and are generally not recommended. If they must be used, ensure that
1343
+ > they can be triggered by a keyboard through other means, such as a button
1344
+ > in the example above.
1345
+
1314
1346
1315
1347
## Language Options
1316
1348
0 commit comments