Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reactable footer total is doubled with paginateSubRows = TRUE and row is expanded #371

Open
vneyman opened this issue Apr 16, 2024 · 1 comment
Labels
doc Documentation issue or suggestion

Comments

@vneyman
Copy link

vneyman commented Apr 16, 2024

Total is doubled for each expanded row when paginateSubRows = TRUE. The below example will show a total of 956.2 vs 478.1.
Is there a way to keep the footer total accurate. Maybe there is a javascript trick?

data <- MASS::Cars93[18:47, ] %>%
select(Manufacturer, Model, Type, Sales = Price)

reactable(
data,
defaultExpanded = TRUE,
paginateSubRows = TRUE,
groupBy = "Manufacturer",
searchable = TRUE,
columns = list(
Manufacturer = colDef(footer = "Total"),
Sales = colDef(
aggregate = "sum",
format = colFormat(currency = "USD"),
footer = JS("function(column, state) {
let total = 0
state.sortedData.forEach(function(row) {
total += row[column.id]
})
return total.toLocaleString('en-US', { style: 'currency', currency: 'USD' })
}")
)
),
defaultColDef = colDef(footerStyle = list(fontWeight = "bold"))
)

@glin
Copy link
Owner

glin commented Apr 22, 2024

There is a way to tell if a row is grouped, but it's not well documented or obvious to use. A row will have a _subRows array property if it's grouped. You can ignore these rows, e.g.:

data <- MASS::Cars93[18:47, ] %>%
  select(Manufacturer, Model, Type, Sales = Price)

reactable(
  data,
  defaultExpanded = TRUE,
  paginateSubRows = TRUE,
  groupBy = "Manufacturer",
  searchable = TRUE,
  columns = list(
    Manufacturer = colDef(footer = "Total"),
    Sales = colDef(
      aggregate = "sum",
      format = colFormat(currency = "USD"),
      footer = JS("function(column, state) {
        let total = 0
        state.sortedData.forEach(function(row) {
          // Ignore grouped rows
          if (row._subRows) {
            return
          }
          total += row[column.id]
        })
        return total.toLocaleString('en-US', { style: 'currency', currency: 'USD' })
      }")
    )
  ),
  defaultColDef = colDef(footerStyle = list(fontWeight = "bold"))
)

This is included in a few examples on the custom rendering doc page, but it could definitely use better documentation.

@glin glin added the doc Documentation issue or suggestion label Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Documentation issue or suggestion
Projects
None yet
Development

No branches or pull requests

2 participants