【问题标题】:How to disable double click reactivity for specific columns in R Datatable如何禁用 R Datatable 中特定列的双击反应性
【发布时间】:2020-06-09 20:39:06
【问题描述】:

我正在我的 Shiny 应用程序中处理一个 DT,其中包含 Shiny Input 对象和一些可编辑的列。

这是一个可重现的例子,


library(shiny)
library(DT)

ui <- fluidPage(
  fluidRow(
  DTOutput('table1')

  )
)

helper_fun <- function(FUN, len, id, ...) {
  inputs = character(len)
  for (i in seq_len(len)) {
    inputs[i] = as.character(FUN(paste0(id, i), selected = 1, ...))
  }
  inputs
}

server <- function(input, output, session) {
  output$table1 <- renderDT({
    dat <- data.frame(Name = c('A', 'B')
                      , column1 = helper_fun(FUN = selectInput, len = length(2), id = 'selector_', label = NULL, choices = c(1, 2),  width="100px") 
                      )
    dat
  }, editable = list(target = 'cell', disable = list(columns = c(2))), server = FALSE, escape = FALSE
  , options = list(preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'), 
                   drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')))
}

shinyApp(ui, server)

我希望能够编辑 1 列并禁用对具有闪亮输入值的列的编辑。使用editable = list(target = 'cell', disable = list(column = c(2))) 不会产生预期的效果,因为这会禁用编辑,但在双击单元格时会暴露底层的html

如何在某些列上禁用这种双击效果?

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    这是一种方法:

    CSS <- "
    table tbody td:nth-child(3) {pointer-events: none;}
    table tbody td:nth-child(3)>div {pointer-events: auto;}
    "
    
    ui <- fluidPage(
      tags$head(tags$style(HTML(CSS))),
      fluidRow(
        DTOutput('table1')
      )
    )
    

    【讨论】:

    • 您好,感谢您的回复。效果很好!你能解释一下为什么 CSS 中的第二行是必要的,删除它似乎不会改变行为吗?
    • @Croote 在我看来,第二行对于选择输入是必要的。
    猜你喜欢
    • 1970-01-01
    • 2019-07-21
    • 2022-11-12
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 2019-08-02
    • 1970-01-01
    • 2018-09-12
    相关资源
    最近更新 更多