简体   繁体   中英

Create an R shiny binding to a double click event on a DT datatable

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.

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});",
  "  }",
  ");"
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM