由于未选中DT :: datatable中的某些复选框,因此无法对其进行预选择

时间:2018-07-19 16:20:39

标签: r shiny dt

我有一个闪亮的应用程序,在其中我希望能够默认选中第一个复选框,例如here。由于某些原因,第一个框仍未选中。

#ui.r
navbarPage(
  "Application",
  tabPanel("General",
           sidebarLayout(

             sidebarPanel(
             ),
             mainPanel(
               DT::dataTableOutput("hot5")

             )
           )))
#server.r
library(shiny)
library(DT)
library(tidyverse)
jsfunc <- "function() {arrIndexes=[0]; $('#hot5 tbody tr').filter(function(index) {return arrIndexes.indexOf(index) > -1;}).click()}"

server <- function(input, output,session) {


  output$hot5 <-DT::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")
    )


  )

}

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

library(shiny)
library(DT)
library(tibble)

ui <- navbarPage(
  "Application",
  tabPanel("General",
           sidebarLayout(

             sidebarPanel(),

             mainPanel(
               DT::dataTableOutput("hot5")
             )
           )
  )
)

js <- "setTimeout(function(){table.row(0).select()},0)" # I don't know why we need the setTimeout

server <- function(input, output,session) {

  output$hot5 <-DT::renderDataTable({
    datatable(iris%>% rowid_to_column("Row") %>% mutate(Row = ""),
              rownames = FALSE,
              extensions = "Select",
              selection = "none",
              callback = JS(js),
              options = list(
                columnDefs = list(list(className = "select-checkbox", targets = 0, orderable = FALSE)),
                select = list(style = "multi", selector = "td:first-child")
              )
    )
  }, server = FALSE)

}

shinyApp(ui, server)