Is it possible to set the tickboxes selected by default in a DT datatable?

妖精的绣舞 提交于 2020-01-14 03:23:28

问题


I would like to ask if it is possible to have the tickboxes of all the rows selected by default -instead of deselected- in a DT datatable.

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

回答1:


Yes, you can write the custom JS Function like so: Here we pre-select the rows 1,3 and 4 (note that the count starts from 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)



来源:https://stackoverflow.com/questions/51304138/is-it-possible-to-set-the-tickboxes-selected-by-default-in-a-dt-datatable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!