问题
How can I pre-select rows with the Select extension for Datatables in Shiny? I've checked the documentation here: https://datatables.net/extensions/select/. But I can't figure it out. I tried specifying rows = 1:3
but that didn't have any effect:
library(DT)
library(shiny)
dat <- iris[1:17,]
shinyApp(
ui = fluidPage(DTOutput("table")),
server = function(input, output, session) {
output[["table"]] <- renderDT({
datatable(
dat,
options = list(select = list(
style = "multi",
rows = 1:3,
selector = "td:not(.notselectable)")),
extensions = "Select", selection = "none")
}, server = FALSE)
}
)
NB. I am using the Select extension for Datatables, not the DT package's implementation of row selection. So selection = list(selected = 1:3)
will not work.
回答1:
There's no select.rows
option. You can use a callback:
output[["table"]] <- renderDT({
datatable(
dat,
callback = JS("table.rows([0,1,2]).select();"),
options = list(select = list(
style = "multi",
selector = "td:not(.notselectable)")),
extensions = "Select", selection = "none")
}, server = FALSE)
来源:https://stackoverflow.com/questions/58944583/r-shiny-pre-selection-of-rows-in-select-extension-for-datatables