使用 rhandsontable 注册回调到 afterOnCellMouseDown 事件

Register callback to afterOnCellMouseDown event with rhandsontable

我有一个 rhandsontable,希望每次单击一个单元格时触发一个事件,这样我就可以识别被单击的单元格。

据我所知,这只能使用 JS 来完成。

我试过使用基本的 .click() 事件,但它使用的是无参数函数,this 是 HTML 本身,没有“selectedCell” 属性 或者你有什么。

library(shiny)
library(shinyjs)
library(rhandsontable)

ui <- fluidPage(
  useShinyjs(),
  actionButton("redraw", "Redraw"),
  rHandsontableOutput("test")
)

server <- function(input, output, session) {
  output$test <- renderRHandsontable({
    input$redraw  # force re-rendering if desired
    
    runjs("$('#test').click(function() {
          console.log(this)
    });")
    
    rhandsontable(data.frame(a = 1:2, b = 3:4))
  })
}

shinyApp(ui, server)

查看 handsontable 文档,我找到了 afterOnCellMouseDown event,这似乎足以满足我的用例,它采用带有 coords 参数的函数,该参数通知我所选单元格。我应该能够将它与 Shiny.onInputChange 结合使用,让我的应用识别被点击的单元格。

但是,我无法弄清楚我需要 运行 绑定到 afterOnCellMouseDown 事件的 JS。正如我提到的,使用 $('#test') 给我 HTML 并且我不知道如何访问 hot 本身。

使用下面的应用程序,单击的单元格的坐标将发送到 input$clickedCelllist(row = i, col = j).

形式的列表
library(shiny)
library(rhandsontable)
library(htmlwidgets)

jsCode <- c(
  "function(el, x) {",
  "  Handsontable.hooks.add('afterOnCellMouseDown', function(event, coords, TD){",
  "    Shiny.setInputValue('clickedCell', coords, {priority: 'event'});",
  "  });",
  "}"
)

ui <- fluidPage(
  rHandsontableOutput("test")
)

server <- function(input, output, session) {
  output$test <- renderRHandsontable({    
    rhandsontable(data.frame(a = 1:2, b = 3:4)) %>% onRender(jsCode)
  })
  observe({print(input$clickedCell)})
}

shinyApp(ui, server)

这适用于应用程序的所有 handsontable 个实例。我不知道如何针对特定目标(我认为这是不可能的)。