【问题标题】:How to set the focus to the first row first column cell in a rhandsontable in an RShiny app?如何将焦点设置到 RShiny 应用程序中 rhandsontable 的第一行第一列单元格?
【发布时间】:2023-02-08 02:43:09
【问题描述】:

我需要将焦点放在 RShiny 应用程序中 rhandsontable 的第一行第一列单元格上。寻找与本论坛中讨论的解决方案类似的解决方案:Set the focus to a specific datagrid cellQML: Set focus TextInput in a table cellhow to focus a table cell using javascript?want to put the focus and edit the first cell of my dynamic table 等。我需要渲染器功能方面的帮助才能完成此任务。

library(shiny)
library(rhandsontable)

DF = data.frame(matrix(data = '', nrow = 5, ncol = 1, dimnames = list(seq(1:5),c("Barcode"))))

ui <- fluidPage(
  titlePanel("Scan Sample Barcode"),
  mainPanel(
    rHandsontableOutput("scanBarcode")
  )
  
)
server <- function(input, output) {
  output$scanBarcode <- renderRHandsontable(rhandsontable(DF) %>%
                                              hot_cols(renderer = "function(instance, td, row, col, prop, value, cellProperties) 
                                                {Handsontable.TextCell.renderer.apply(this, arguments);
                                                if (col == 0 & row == 0 ) {td.focus();}")
                                           )
}

shinyApp(ui = ui, server = server)

【问题讨论】:

  • 如果您提供最少的构建代码,您可能会得到更多帮助。
  • 我按照建议添加了代码。我想添加 JavaScript 作为渲染器。
  • 谢谢指出错误。我按照建议更新了代码。

标签: javascript r shiny rhandsontable


【解决方案1】:

他的呢:

library(htmlwidgets)

server <- function(input, output) {
  output$scanBarcode <- renderRHandsontable(
    rhandsontable(DF) %>% 
    onRender("function(el, x) { 
                 let hot = this.hot;
                 hot.selectCell(0, 0); 
                 hot.getActiveEditor().beginEditing();
             }")  
  )
}

您使用 handsontable API 选择单元格 (注意索引在 JS 中为零),然后调用单元格编辑器。

您将此 JS 代码放在 onRender 函数中,您可以在其中引用正在创建的 handsontable 对象。

【讨论】:

  • 极好的!你让它看起来很简单。该解决方案将对像我这样的许多用户有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-29
  • 2018-10-04
  • 1970-01-01
  • 2019-01-08
  • 1970-01-01
  • 1970-01-01
  • 2018-03-23
相关资源
最近更新 更多