【问题标题】:Is it possible to set the tickboxes selected by default in a DT datatable?是否可以在 DT 数据表中设置默认选择的复选框?
【发布时间】:2018-07-12 11:11:04
【问题描述】:

我想问是否可以在 DT 数据表中默认选中所有行的复选框 - 而不是取消选中 -。

library(DT)
library(tidyverse)
dataTableOutput("irisTable")
output$irisTable <- renderDataTable(
    iris %>% rowid_to_column("Row") %>% mutate(Row = ""),
    rownames = FALSE,
    extensions = "Select",
    options = list(
        columnDefs = list(list(className = "select-checkbox", targets = 0, orderable = FALSE)),
        select = list(style = "multi", selector = "td:first-child")
        ))

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    是的,你可以像这样编写自定义的JS 函数: 这里我们预选了第1,3和4行(注意计数是从0开始的)

    library(DT)
    library(tidyverse)
    library(shiny)
    
    ui <- fluidPage(
      dataTableOutput("irisTable")
    )
    
    jsfunc <- "function() {arrIndexes=[1,3,4]; $('#irisTable tbody tr').filter(function(index) {return arrIndexes.indexOf(index) > -1;}).click()}"
    
    server <- function(input, output){
    
      output$irisTable <- renderDataTable(
        iris %>% rowid_to_column("Row") %>% mutate(Row = ""),
        rownames = FALSE,
        extensions = "Select",
        options = list(
          initComplete = JS(jsfunc),
          columnDefs = list(list(className = "select-checkbox", targets = 0, orderable = FALSE)),
          select = list(style = "multi", selector = "td:first-child")
        ))
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 我复制并粘贴了您的代码,但它不起作用。可能是什么问题?
    • @Pork Chop,你将如何预先检查所有行?
    猜你喜欢
    • 2018-12-18
    • 2021-05-12
    • 2015-02-10
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 2021-04-26
    • 2021-03-09
    • 2013-12-01
    相关资源
    最近更新 更多