简体   繁体   English

创建一个 R shiny 绑定到 DT 数据表上的双击事件

[英]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.我正在尝试通过使用 R shiny 绑定双击 DT 数据表来创建操作。 I have no noticeable JS skills so I admit that is the problem;-).我没有明显的 JS 技能,所以我承认这是问题所在;-)。 From examples on SO I have scraped the following code together (code snippet and simplified to show the problem):从 SO 上的示例中,我将以下代码拼凑在一起(代码片段并简化以显示问题):

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 .我正在尝试创建与 shiny input object ( input$dt_dblclick )和使用Shiny.setInputValue的双击事件的绑定。 But nothing happens.但什么也没有发生。 No values are printed to the console.没有值被打印到控制台。 Please help.请帮忙。 Thanks.谢谢。

You have to use td and not tr .您必须使用td而不是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});",
  "  }",
  ");"
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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