css 有没有办法不把rhandsontableheight设置为一个很大的值,以便正确地呈现下拉菜单?

oxosxuxt  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(144)

运行本文末尾的代码会显示一个带有下拉选项的rhandsontable。让下拉选项出现的唯一方法是设置一个非常大的表高,但这会产生将actionButton()向下推得太远的丑陋效果。如果注解掉表高行(在代码中用# <<标注),actionButton()位置解析,但表格下拉列表不再呈现,如下图所示。有没有一种方法可以让下拉菜单覆盖(或悬停)任何呈现在它们下面的对象,这样你就不必求助于这种愚蠢的表格高度了?
一个解决方案是将actionButton()移到顶部,但在更完整的应用中,这是为了有一系列操作按钮,这些按钮有条件地呈现它们下面的重要对象,因此将actionButton()移到顶部是不可行的,它必须留在table下面。
说明:

代码:

library(shiny)
library(rhandsontable)

ui <- fluidPage(br(),
  mainPanel(
    rHandsontableOutput("Tbl"),br(),br(),
    actionButton("add", "Add column"),
    )
  )

server <- function(input, output) {
  DF <- reactiveVal(
    data.frame(
      'Series 1' = NA_character_, 
      stringsAsFactors = FALSE,
      row.names = c("Select option"),
      check.names = FALSE
      )
    )
  
  observeEvent(input$Tbl,{DF(hot_to_r(input$Tbl))})
  
  output$Tbl <- renderRHandsontable({
    select_option <- c(NA_character_, "A","B","C","D","E","F","G","H","I","J","K")
    tmp <- rhandsontable(
      DF(), 
      rowHeaderWidth = 200, 
      selectCallback = TRUE, 
      height = 300 # << comment this line out to correctly position the action button
      ) %>%
    hot_cols(colWidths = 100) %>%
    hot_col("Series 1", 
            allowInvalid = FALSE, 
            type = "dropdown", 
            source = NA_character_, 
            readOnly = TRUE
            )
    tmp <- hot_col(tmp, 
                   col = names(DF()), 
                   allowInvalid = FALSE, 
                   type = "dropdown", 
                   source = select_option
                   ) %>% 
      hot_cell(row = input$Tbl_select$select$r, col = "Series 1", readOnly = FALSE)
    tmp
  })
  
  observeEvent(input$add, {
    newCol <- data.frame('Series 1' = NA_character_,stringsAsFactors = FALSE)
    names(newCol) <- paste("Series", ncol(hot_to_r(input$Tbl)) + 1)
    DF(cbind(DF(), newCol))
  })
  
}

shinyApp(ui = ui, server = server)
olmpazwi

olmpazwi1#

height = 300更改为overflow = "visible"可解决问题!

相关问题