R Shiny - Pre-selection of rows in Select extension for Datatables

做~自己de王妃 提交于 2021-02-11 18:06:15

问题


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

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