[英]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. 這是示例 shiny應用程序
以下是該應用程序的可重現代碼:
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)
我想我可能必須添加一些自定義 javascript 來解決這個問題,但我不擅長它。 任何人都可以幫助或提供任何建議。
謝謝
不知何故,我設法找出了我的問題的解決方案。 把它貼在這里,所以它可能會幫助其他人。 我從幾個地方得到了幫助。 數據表文檔和stackoverflow
使用這些幫助,我擴展了我的全選按鈕功能,並將它擴展為取消全選按鈕(取消選擇任何過濾的行)。
這是更新的 shiny應用程序
以下是更新后的代碼:
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)
希望這對其他人有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.