I have rather simple application (below) where I try to output grouped table using DataTable with the ability to collapse the groups. I found solution in that is implemented in jQuery here but I have no idea how such complex implementation could be moved into R.
Currently, I am able to collapse within a group but not the whole group itself. Any hints how this could be implemented in Shiny?
My application:
library(shiny)
library(DT)
library(shinyjs)
ui <- fluidPage(
# Application title
titlePanel("Collapse/Expand table"),
mainPanel(
DTOutput("my_table")
)
)
server <- function(input, output) {
output$my_table<-DT::renderDataTable({
datatable(mtcars[1:15,1:5],
extensions = 'RowGroup',
options = list(rowGroup = list(dataSrc=c(3)),
pageLength = 20),
callback = JS("
table.on('click', 'tr', function () {
var rowsCollapse = $(this).nextUntil('.group');
$(rowsCollapse).toggleClass('hidden');
});"))
})
}
# Run the application
shinyApp(ui = ui, server = server)
EDIT
Given AEF comment one can adjust the code to specify that even has to take place once table body is clicked. This indeed collapses any rows until next group. The remaining part is to restrict the clicks only onto group rows. The callback should be now:
callback = JS("$('#DataTables_Table_0 tbody').on('click', 'tr', function () {
$(this).nextUntil('.group').toggleClass('hidden');});"))
javascript
tag to your question to see if you can get extra help, maybe a question on github.com/rstudio/DT? – David Jorqueraon
function is not respected. – AEF