r - Shiny 的数据表 : Disable row selection for certain rows

标签 r datatables shiny dt

我正在尝试确定 shiny 是否可能DataTable对某些行禁用行选择。

使用 selection DT::datatable 的参数我可以预先选择行,确定用户是选择行还是列或两者,并完全禁用选择,但我不清楚是否可以指示要排除的特定行。这可能吗?

问候

最佳答案

Select你可以做的扩展:

library(DT)
library(shiny)

dat <- iris[1:17,]

rowCallback <- c(
  "function(row, data, displayNum, displayIndex){",
  "  var indices = [0, 2, 4, 15];",
  "  if(indices.indexOf(displayIndex) > -1){",
  "    $(row).find('td').addClass('notselectable');",
  "  }",
  "}"
)

shinyApp(
  ui = fluidPage(
    DTOutput("table")
  ),
  server = function(input, output, session) {    
    output[["table"]] <- renderDT({
      dat %>%
        datatable(options = list(
          rowCallback = JS(rowCallback), 
          select = list(style = "multi", selector = "td:not(.notselectable)")
        ), 
        extensions = "Select", selection = "none"
        )
    }, server = FALSE)
  }
)

但是,如果您需要 input$table_rows_selected 中所选行的索引,你必须用 JavaScript 编码:
callback <- c(
  "var id = $(table.table().node()).closest('.datatables').attr('id');",
  "table.on('click', 'tbody', function(){",
  "  setTimeout(function(){",
  "    var indexes = table.rows({selected:true}).indexes();",
  "    var indices = Array(indexes.length);",
  "    for(var i = 0; i < indices.length; ++i){",
  "      indices[i] = indexes[i];",
  "    }",
  "    Shiny.setInputValue(id + '_rows_selected', indices);",
  "  }, 0);",
  "});"
)

shinyApp(
  ui = fluidPage(
    DTOutput("table")
  ),
  server = function(input, output, session) {    
    output[["table"]] <- renderDT({
      dat %>%
        datatable(
          callback = JS(callback),
          options = list(
            rowCallback = JS(rowCallback), 
            select = list(style = "multi", selector = "td:not(.notselectable)")
          ), 
          extensions = "Select", selection = "none"
        )
    }, server = FALSE)
    observe({
      print(input[["table_rows_selected"]])
    })
  }
)

enter image description here

关于r - Shiny 的数据表 : Disable row selection for certain rows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35490910/

相关文章:

R Shiny 为 Plotly 折线图选择轨迹?

r - 在 docx 中为 flextable 添加标题

r - 为什么我收到错误 : argument of length 0

r - 为每个 ID 查找重叠日期并为重叠创建一个新行

带有 twitter bootstrap 的 javascript 数据表

javascript - jQuery Datatables sDom 将 columnFilterWidget 推到左侧 (Firefox)

javascript - 行更改页脚中的数据表动态总计

r - 有没有办法将几个 excel 文件从 Dropbox 文件夹加载到 R-shiny 应用程序中?

r - 如何隐藏或禁用pickerInput中的一项选择多项

R:保留 1 行/列矩阵