Aggregation and Grouping Feature Guide
Mantine React Table has built-in grouping and aggregation features. There are options for both automatic client-side grouping and aggregation, as well as manual server-side grouping and aggregation. This guide will walk you through the different options and how to use and customize them.
Relevant Table Options
Relevant Column Options
# | Column Option | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 | |||||
2 | MRT Data Columns Docs | ||||
3 | |||||
4 | |||||
Relevant State
# | State Option | Type | Default Value | More Info Links | |
---|---|---|---|---|---|
1 | TanStack Table Expanding Docs | ||||
2 | TanStack Table Grouping Docs | ||||
Enable Grouping
To enable grouping, set the
enableGrouping
table option to true
. This will both add a drag handle button so that columns can be dragged to the dropzone to be grouped and will add an entry column actions menu to group or ungroup a column.Disable Grouping Per Column
Hide Drag Buttons for Grouping
If you do not want the drag buttons that come with the grouping feature, you can independently disable them without disabling the grouping feature entirely by setting the
enableColumnDragging
table option to false
.Group Columns by Default
If you want columns to be grouped by default, you can set the
grouping
state in either the initialState
or state
table options.Expand Grouped Rows by Default
In addition to grouping columns by default, you may also want those grouped rows to be expanded and visible by default, too. You can do this by setting the
expanded
state to true
in either the initialState
or state
table options.Aggregation on Grouped Rows
One of the cool features of Mantine React Table is that it can automatically aggregate the data in grouped rows. To enable this, you must specify both an
aggregationFn
and an AggregatedCell
render option on a column definition.Built-in Aggregation Functions
There are several built-in aggregation functions available that you can use. They are:
count
- Finds the number of rows in a groupextent
- Finds the minimum and maximum values of a group of rowsmax
- Finds the maximum value of a group of rowsmean
- Finds the average value of a group of rowsmedian
- Finds the median value of a group of rowsmin
- Finds the minimum value of a group of rowssum
- sums the values of a group of rowsuniqueCount
- Finds the number of unique values of a group of rowsunique
- Finds the unique values of a group of rows
All of these built-in aggregation functions are from TanStack Table
Custom Aggregation Functions
If none of these pre-built aggregation functions work for you, you can also pass in a custom aggregation function. The aggregation function will be passed in an array of values from the column that you are aggregating. It should return a single value that will be displayed in the aggregated cell.
If you are specifying a custom aggregation function, it must implement the following type:
Aggregation on All Rows in Footer
Mantine React Table does not automatically aggregate all rows for you to calculate totals for the entire table. However, it is still easy enough to do this manually and add in your custom calculations into the
footer
or Footer
of a column definition. It is recommended that you do any necessary aggregation calculations on your data in a useMemo hook before passing it to the columns footer in your columns definition.Please remember to perform heavy aggregation calculations in a useMemo hook to avoid unnecessary re-renders!
Custom Cell Renders for Aggregation and Grouping
There are a few custom cell render overrides that you should be aware of when using grouping and aggregation features.
AggregatedCell Column Option
"Aggregation Cells" are cells in an aggregated row (not a normal data row) that can display aggregates (avg, sum, etc.) of the data in a group. The cell that the table is grouped on, however, is not an Aggregate Cell, but rather a GroupedCell.
You can specify the custom render for these cells with the
AggregatedCell
render option on a column definition.GroupedCell Column Option
"Grouped Cells" are cells in a grouped row (not a normal data row) that by default display the value that the rows are grouped on and the number of rows in the group. You can override the default render for these cells with the
GroupedCell
render option on a column definition.PlaceholderCell Column Option
"Placeholder Cells" are cells that are usually meant to be empty in grouped rows and columns. They are simply rendered with a value of
null
by default, but you can override the default render for these cells with the PlaceholderCell
render option on a column definition.Aggregation/Grouping Example
State | First Name | Last Name | Age | Gender | Salary | |
---|---|---|---|---|---|---|
Alabama (2) | Oldest by State: 42 | Average by State: $71,105 | ||||
Celine | Johnston | 42 | Non-binary | $96,445 | ||
Harmon | Bode | 14 | Female | $45,764 | ||
Alaska (7) | Oldest by State: 79 | Average by State: $61,315 | ||||
Juliet | Upton | 13 | Male | $43,447 | ||
Martina | Miller | 79 | Male | $59,714 | ||
Herbert | Lebsack | 33 | Female | $68,645 | ||
Vivian | Rempel | 30 | Female | $89,537 | ||
Rick | Will | 36 | Female | $48,064 | ||
Guiseppe | Heller | 31 | Female | $20,924 | ||
Ben | Dooley | 34 | Non-binary | $98,876 | ||
Arizona (8) | Oldest by State: 77 | Average by State: $66,775 | ||||
Samantha | Dibbert | 48 | Male | $31,884 | ||
Jevon | Wuckert | 52 | Male | $88,064 | ||
Lia | Lowe | 77 | Male | $56,479 | ||
Abagail | Luettgen | 25 | Male | $99,154 | ||
Alanis | Kuhic | 5 | Male | $93,668 | ||
Arvid | Auer | 8 | Male | $59,826 | ||
Rosella | Blanda | 54 | Female | $76,754 | ||
Nathen | Waelchi | 55 | Female | $28,373 | ||
Max Age: 80 | Average Salary: $57,062 |
1-20 of 298
Multiple Aggregations Per column
You may want to calculate more than one aggregation per column. If so, you can specify an array of
aggregationFn
s, and then reference the aggregation results from an array in the AggregatedCell
render option.State | Gender | First Name | Last Name | Salary | |
---|---|---|---|---|---|
Alabama (3) | Count: 7 Average: $43,375 Median: $56,146 Min: $10,645 Max: $71,238 | ||||
Female (4) | Count: 4 Average: $40,264 Median: $43,339 Min: $10,645 Max: $63,733 | ||||
Thad | Wiegand | $56,146 | |||
Reinhold | Reichel | $30,531 | |||
Lurline | Koepp | $10,645 | |||
Kody | Braun | $63,733 | |||
Male (2) | Count: 2 Average: $41,915 Median: $41,915 Min: $12,591 Max: $71,238 | ||||
Alivia | Ledner | $12,591 | |||
Danyka | Gleason | $71,238 | |||
Nonbinary (1) | Count: 1 Average: $58,743 Median: $58,743 Min: $58,743 Max: $58,743 | ||||
Lionel | Hartmann | $58,743 | |||
Alaska (2) | Count: 8 Average: $68,901 Median: $75,746 Min: $35,159 Max: $98,252 | ||||
Male (4) | Count: 4 Average: $65,414 Median: $67,396 Min: $45,801 Max: $81,062 | ||||
Eloisa | Kohler | $45,801 | |||
Kian | Hand | $81,062 | |||
Michale | Collier | $75,197 | |||
Eldridge | Stroman | $59,594 | |||
Female (4) | Count: 4 Average: $72,388 Median: $78,070 Min: $35,159 Max: $98,252 | ||||
Loyce | Schmidt | $76,295 | |||
Alvera | Balistreri | $79,844 |
1-20 of 342
Manual Grouping
Manual Grouping means that the
data
that you pass to the table is already grouped and aggregated, and you do not want Mantine React Table to do any of the grouping or aggregation for you. This is useful if you are using a backend API to do the grouping and aggregation for you, and you just want to display the results. However, you will need to put your data in the specific format that the expanding
features understand.