如何在闪亮的DT数据表中预先选择行?

时间:2022-05-07 14:26:15

I'm using Shiny (0.12.0) with DT (0.0.65) for row-selections in this Shiny datatable. I want to pre-select the first 5 rows. I have tried:

在这个闪亮的数据表中,我使用了闪亮(0.12.0)和DT(0.0.65)作为行选择。我要预先选择前5行。我有尝试:

  • Changing the class of row using callback JS in datatable. However, that is not reflecting in the input$x1_rows_selected variable. Only the background/highlight changes because of CSS.
  • 使用datatable中的回调JS更改行类。但是,这并不反映在输入$x1_rows_selected变量中。只有背景/突出显示因为CSS而改变。
  • Using .click() in either rowCallback in the options list or in callback. This does not work either when loading the page. However, it works (updates input$x1_rows_selected) when I run the same code through the console / browser dev tool.
  • 在选项列表或回调中使用.click()。这在加载页面时也不起作用。但是,当我通过控制台/浏览器开发工具运行相同的代码时,它可以工作(更新输入$x1_rows_selected)。

callback JS:

回调JS:

output$x1 = DT::renderDataTable({
    datatable(cars,
        rows = $("#x1 tbody tr");
        $(rows).slice(0,5).each(function() {
            $(this).click();
        });
    )
})

1 个解决方案

#1


4  

This feature has been added to DT (>= 0.1.3). Examples:

这个特性已经添加到DT(>= 0.1.3)中。例子:

library(shiny)
if (packageVersion('DT') < '0.1.3') devtools::install_github('rstudio/DT')
library(DT)
shinyApp(
  ui = fluidPage(
    fluidRow(
      h1('Client-side processing'),
      DT::dataTableOutput('x1'),
      h1('Server-side processing'),
      DT::dataTableOutput('x2')
    )
  ),
  server = function(input, output, session) {
    output$x1 = DT::renderDataTable(
      iris, server = FALSE,
      selection = list(mode = 'multiple', selected = c(1, 3, 8, 12))
    )
    output$x2 = DT::renderDataTable(
      iris, server = TRUE,
      selection = list(mode = 'multiple', selected = rownames(iris)[c(1, 3, 8, 12)])
    )
  }
)

#1


4  

This feature has been added to DT (>= 0.1.3). Examples:

这个特性已经添加到DT(>= 0.1.3)中。例子:

library(shiny)
if (packageVersion('DT') < '0.1.3') devtools::install_github('rstudio/DT')
library(DT)
shinyApp(
  ui = fluidPage(
    fluidRow(
      h1('Client-side processing'),
      DT::dataTableOutput('x1'),
      h1('Server-side processing'),
      DT::dataTableOutput('x2')
    )
  ),
  server = function(input, output, session) {
    output$x1 = DT::renderDataTable(
      iris, server = FALSE,
      selection = list(mode = 'multiple', selected = c(1, 3, 8, 12))
    )
    output$x2 = DT::renderDataTable(
      iris, server = TRUE,
      selection = list(mode = 'multiple', selected = rownames(iris)[c(1, 3, 8, 12)])
    )
  }
)