简体   繁体   English

Shiny DT,自定义按钮到 select 过滤行

[英]Shiny DT, custom button to select filtered rows

I would like to include custom buttons as extensions to the data.table in the minimal reproducible example below.我想在下面的最小可重现示例中包含自定义按钮作为data.table的扩展。 This example is essentially taken from this question .这个例子基本上取自这个问题

As I have several data.tables in my application, I tried to generalize the node in the JS code of the action definition of the button with the function var table = $(this.api().table().node()).DataTable();由于我的应用程序中有几个data.tables ,我尝试使用 function var table = $(this.api().table().node()).DataTable();来概括按钮action定义的JS代码中的节点var table = $(this.api().table().node()).DataTable(); , which does not work. ,这是行不通的。 If I insert var table = $('#DataTables_Table_0').DataTable();如果我插入var table = $('#DataTables_Table_0').DataTable(); it works fine, but is not general any more.它工作正常,但不再通用。

Any help is really appreciated!非常感谢任何帮助!

library(shiny)
library(DT)

ui <- fluidPage(

  titlePanel("Select only filtered rows using selectall button"),

  br(),
  br(),

  DT::dataTableOutput("tableTest")
)

# Define server logic required to draw a histogram
server <- function(input, output) {

  output$tableTest <- DT::renderDataTable({

    datatable(mtcars,
      escape = F,
      rownames = F,
      filter = 'top',
      class = "compact hover row-border",
      extensions = c('Scroller', 'Select', 'Buttons'),

      options = list(
        select = list(style = "multi", items = "row"),
        columnDefs = list(list(className = 'dt-center', targets = "_all")),
        language = list(info = 'Showing _START_ to _END_ of _TOTAL_ variables'),
        deferRender = TRUE,
        scrollY = 500,
        scroller = TRUE,
        dom = "Blfrtip",
        buttons =
          list(
            list(extend='selectAll',
              className='selectAll',
              text="select all rows",
              action = DT::JS(
                "function () {
                var table = $(this.api().table().node()).DataTable();
                table.rows({ search: 'applied'}).deselect();
                table.rows({ search: 'applied'}).select();
              }")
            ),
            list(extend='selectNone',
              text="DeselectAll",
              action = DT::JS(
                "function () {
                var table = $(this.api().table().node()).DataTable();
                table.rows({ search: 'applied'}).select();
                table.rows({ search: 'applied'}).deselect();
              }")
            ))
      ),
      selection = "none"
    ) }, server = F
  )
}

# Run the application
shinyApp(ui = ui, server = server)


I have not tried, but I think you can do:我没有尝试过,但我认为你可以这样做:

action = JS(
  "function(e, table, node, config) {",
  "  table.rows({ search: 'applied'}).deselect();",
  "  table.rows({ search: 'applied'}).select();",
  "}"
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM