【问题标题】:Shiny DataTable: Disable row selection for certain rowsShiny DataTable:禁用某些行的行选择
【发布时间】:2016-05-31 04:14:09
【问题描述】:

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

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

问候

【问题讨论】:

    标签: r datatables shiny dt


    【解决方案1】:

    使用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"]])
        })
      }
    )
    

    【讨论】:

    • 感谢您发布此解决方案@Stéphane,它真的帮助了我。我想知道如何将行号的 R 向量(例如 disabled = c(1,2,3))传递给 rowCallback?我和paste0(" var indices = ", toJSON(disabled), ";") 相处得很好,但它看起来很老套。
    • @user51462 你可以做sprintf(" var indices = [%s];", paste0(disabled, collapse = ",")
    • 这仍然适用于列排序吗?假设您根据Species 按降序对行进行排序,然后第 101、103、105 和 116 行将被禁用,因为它们的displayIndex 现在由于排序结果分别为 0、2、4 和 15。但是,如果我想根据行名禁用行,以便始终禁用 1、3、5 和 16,而不管它们的 displayIndex 是什么?
    • 很好用。但是我们如何才能为特定列做到这一点呢?
    • 我刚刚为" $(row).find('td:nth-child(1)').addClass('notselectable');",987654333@找到了一个解决方案
    猜你喜欢
    • 2021-01-23
    • 1970-01-01
    • 2011-01-17
    • 2019-09-12
    • 2017-09-30
    • 2022-01-09
    • 1970-01-01
    • 2018-03-04
    • 1970-01-01
    相关资源
    最近更新 更多