Reputation: 1495
I am trying to create an action from double clicking on a DT datatable using R shiny bindings. I have no noticeable JS skills so I admit that is the problem ;-). From examples on SO I have scraped the following code together (code snippet and simplified to show the problem):
library(shiny)
library(DT)
shiny::shinyServer(
function(input, output, session) {
data <- reactiveValues(data = NA)
data$data <- "some code to make the data"
output$table <- DT::renderDataTable({
DT::datatable(
data = data$data,
class = 'compact',
escape = FALSE,
rownames = FALSE,
selection = "single",
options = (
list(
dom = "t",
ordering = FALSE,
paging = FALSE,
autoWidth = FALSE,
scrollY = "100vh",
scrollCollapse = FALSE
)
),
callback = htmlwidgets::JS(" table.on('dblclick','tr',
function() {
var row_=table.cell(this).index().row;
var col=table.cell(this).index().column;
Shiny.setInputValue('dt_dblclick', {"dt_row": row_, "dt_col": col});
}
);")
)
}, server = FALSE)
observeEvent(input$dt_dblclick, {
print(input$dt_dblclick)
})
})
}
I am trying to create a binding to a shiny input
object (input$dt_dblclick
) and the double click event using Shiny.setInputValue
. But nothing happens. No values are printed to the console.
Please help. Thanks.
Upvotes: 1
Views: 1308
Reputation: 84709
You have to use td
and not tr
. And remove the double quotes which cause a syntax error.
JS(
"table.on('dblclick', 'td',",
" function() {",
" var row = table.cell(this).index().row;",
" var col = table.cell(this).index().column;",
" Shiny.setInputValue('dt_dblclick', {dt_row: row, dt_col: col});",
" }",
");"
)
Upvotes: 2