首页 > 解决方案 > 如何禁用 R Datatable 中特定列的双击反应性

问题描述

我正在我的 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双击单元格时会暴露底层。

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

标签: rshinydt

解决方案


这是一种方法:

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')
  )
)

推荐阅读