代码之家  ›  专栏  ›  技术社区  ›  RanonKahn

如何在RShiny应用程序中将焦点设置为rhandson表中的第一行第一列单元格?

  •  0
  • RanonKahn  · 技术社区  · 3 年前

    我需要将重点放在RShiny应用程序中rhandontable的第一行第一列单元格上。正在寻找与本论坛讨论的解决方案类似的解决方案: Set the focus to a specific datagrid cell , QML: Set focus TextInput in a table cell , how 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)
    

    正在寻找如下所示的解决方案: enter image description here

    0 回复  |  直到 3 年前
        1
  •  1
  •   thothal    3 年前

    他的呢:

    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 函数,其中您有正在创建的可手持对象的引用。

    Animated screenshot showing a table where the first cell is selected and the editor is invoked on loading