问题
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