Skip to content

Instantly share code, notes, and snippets.

@jonocarroll
Last active May 4, 2023 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonocarroll/6de8d5b4b7dbe33001820ba1939ebac0 to your computer and use it in GitHub Desktop.
Save jonocarroll/6de8d5b4b7dbe33001820ba1939ebac0 to your computer and use it in GitHub Desktop.
Expand/Collapse buttons plus rowcounts and totals for rowGroups in DT::datatable
library(shiny)
ui <- fluidPage(
titlePanel("Expand/Collapse DataTable"),
mainPanel(
fluidRow(
column(6, DT::dataTableOutput("tbl")),
column(6, DT::dataTableOutput("tbl2"))
)
)
)
## start collapsed
callback_js <- DT::JS(
"table.on('click', 'tr.dtrg-group', function () {",
" var rowsCollapse = $(this).nextUntil('.dtrg-group');",
" $(rowsCollapse).toggleClass('hidden');",
"});",
"table.on('init', () => $('.dtrg-group').trigger('click'))"
)
server <- function(input, output) {
set.seed(1)
d <- data.frame(Label = c("A", "A", "B", "B", "B", "C", "C", "C", "C"),
Value = sample(9))
# Use the ID of the table in the JS. In this example, the first table is DataTables_Table_1
output$tbl <- DT::renderDT({
DT::datatable(
d,
extensions = c("RowGroup", "Buttons"),
options = list(
rowGroup = list(dataSrc = 1,
startRender = DT::JS("function(rows, group) {
var nrows = 0;
var total = 0;
rows.rows().data().filter(function(value, index) {
nrows += value[1] === group ? 1 : 0;
total += value[1] === group ? value[2] : 0;
});
return group + ' (' + nrows + ' rows; total value: ' + total + ')';
}")
),
pageLength = 20,
dom = 'Bfrtlip',
buttons = list(
list(extend = "",
text = 'Expand <span class="glyphicon glyphicon-plus"></span>',
action = DT::JS("() => {
$('#DataTables_Table_1').find('.hidden').removeClass('hidden');
}")
),
list(extend = "",
text = 'Collapse <span class="glyphicon glyphicon-minus"></span>',
action = DT::JS("() => {
$('#DataTables_Table_1').find('.even').addClass('hidden');
$('#DataTables_Table_1').find('.odd').addClass('hidden');
}")
)
)
),
callback = callback_js,
selection = 'none',
escape = FALSE
)
})
output$tbl2 <- DT::renderDT({
DT::datatable(
d,
extensions = c("RowGroup"),
options = list(
rowGroup = list(dataSrc = 1)
),
callback = callback_js,
selection = 'none',
escape = FALSE
)})
}
# Run the application
shinyApp(ui = ui, server = server)
@jonocarroll
Copy link
Author

jonocarroll commented May 3, 2023

I couldn't find the way to do this documented anywhere so once I figured it out, I thought I'd share.

  • Group rows by a column
  • Collapse/Expand all grouped rows
  • Add row counts and totals to the group headers
Screen.Recording.2023-05-03.at.1.59.26.pm.mov

Known limitations:

  • To collapse/expand, this just adds the 'hidden' class to any element of class 'odd' or 'even' (or entirely removes the 'hidden' class from any element of class 'hidden') - this isn't specific to any one table or element, so take care if anything else has that class Table ID can now be specified which limits the operations to that table.
Screen.Recording.2023-05-03.at.4.09.22.pm.mov

Update: Use Array.from() to make the hid HTMLCollection non-lazy otherwise when the DOM is updated each iteration, the rows with 'hidden' class won't align.

@jonocarroll
Copy link
Author

Additional update: using jQuery .find('.hidden').removeClass('hidden') works even better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment