简体   繁体   English

Select 仅使用 select 过滤的行 select 扩展中的所有按钮

[英]Select only filtered rows using select all button that comes with select extension in shiny's DT package

I am trying to select only filtered rows using select all button that comes with select extension in shiny's DT package but it selects all the rows. I am trying to select only filtered rows using select all button that comes with select extension in shiny's DT package but it selects all the rows. Here's the sample shiny app这是示例 shiny应用程序

Below is the reproducible code for the app:以下是该应用程序的可重现代码:

library(DT)
data(mpg)
# Define UI for application 
ui <- fluidPage(

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

    br(),
    br(),

    DT::dataTableOutput("table")


)

# Define server logic 
server <- function(input, output) {

    output$table <- DT::renderDataTable({
        datatable(mpg, escape=F,
                  rownames=F,
                  filter = 'top',
                  #  colnames = c("Data Type","Variable","Description", "Filename"),
                  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 = c('selectAll', 'selectNone')
                  ),
                  selection="none"
        ) }, server = F
    )
}

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

I think I may have to add some custom javascript to fix this but I am not good with it.我想我可能必须添加一些自定义 javascript 来解决这个问题,但我不擅长它。 Can anyone help or give any suggestions.任何人都可以帮助或提供任何建议。

Thanks谢谢

Somehow I managed to figure out the solution for my question.不知何故,我设法找出了我的问题的解决方案。 Posting it here, so it might help others.把它贴在这里,所以它可能会帮助其他人。 I got help from couple of places.我从几个地方得到了帮助。 Datatable document and stackoverflow 数据表文档stackoverflow

Using these helps, I extended my selectall button functionality and also extended it for deselectall button (deselect any filtered rows).使用这些帮助,我扩展了我的全选按钮功能,并将它扩展为取消全选按钮(取消选择任何过滤的行)。

Here's the updated shiny app这是更新的 shiny应用程序

Below is the updated code:以下是更新后的代码:

library(shiny)
library(DT)
data(mpg)
# Define UI for application that draws a histogram
ui <- fluidPage(

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

    br(),
    br(),

    DT::dataTableOutput("table")



)

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

    output$table <- DT::renderDataTable({
        datatable(mpg, escape=F,
                  rownames=F,
                  filter = 'top',
                  #  colnames = c("Data Type","Variable","Description", "Filename"),
                  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 = $('#DataTables_Table_0').DataTable();
                                table.rows({ search: 'applied'}).deselect();
                                table.rows({ search: 'applied'}).select();
                }")
                                ), list(extend='selectNone',
                                        text="DeselectAll",
                                        action=DT::JS("function () {
                                var table = $('#DataTables_Table_0').DataTable();
                                table.rows({ search: 'applied'}).select();
                                table.rows({ search: 'applied'}).deselect();
                }")
                                ))

                  ),
                  selection="none"
        ) }, server = F
    )
}

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

Hope this help others.希望这对其他人有帮助。

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

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