Blazor Grid Group By
A Group By behavior in an Blazor Material table or UI grid creates grouped data rows based on the column values. The Group By in the IgbGrid
allows for visualizing the groups in a hierarchical structure. The grouped data rows can be expanded or collapsed and the order of grouping may be changed through the UI or API. When Row Selection is enabled, a Group By row selector is rendered in the left-most area of the group row. In case the RowSelection
property is set to single, checkboxes are disabled and only serve as an indication for the group where selection is placed. If the RowSelection
property is set to multiple, clicking over the Group By row selector selects all records belonging to this group.
Blazor Grid Group By Example
This example presents the grouping capabilities of a large amount of data. Dragging the column headers to the top (grouping area) allows users to see the data for the selected column in a hierarchical structure. They can do group by in multiple fields by dragging more column headers to the top. These grouping options come in handy when you have tables with numerous rows and columns where users want to present the data in a much faster and visually acceptable way.
Initial Grouping State
It is possible to define initial grouping of the grid by assigning an array of expressions to the GroupingExpressions
property of the grid.
Grouping expressions implement the ISortingExpression
interface.
Group By API
Grouping API
Grouping is available through the UI and through a robust API exposed by the grid component. Developers can allow end-users to group the grid data by certain columns, by setting each column's Groupable
property to true
.
During runtime the expressions are gettable and settable from the groupingExpressions
property. If you need to add or change an existing expression you may also use the GroupBy
method with either a single or an array of ISortingExpression
.
Expand/Collapse API
In addition to grouping expressions you can also control the expansion states for group rows. They are stored in a separate property of the IgbGrid
component GroupingExpansionState
. A group row is uniquely identified based on the field name it is created for and the value it represents for each level of grouping. This means that the signature of an expansion state interface is the following:
As with GroupingExpressions
, setting a list of IGroupByExpandState
directly to the GroupingExpansionState
will change the expansion accordingly. Additionally IgbGrid
exposes a method that toggles a group by the group record instance.
Groups can be created expanded (default) or collapsed and the expansion states would generally only contain the state opposite to the default behavior. You can control whether groups should be created expanded or not through the GroupsExpanded
property.
Select/Deselect All Rows in a Group API
Selecting/Deselecting all rows in a group is available through the SelectRowsInGroup
and DeselectRowsInGroup
API methods.
The code snippet below can be used to select all rows within a group using the group record instance SelectRowsInGroup
method. Additionally, the second parameter of this method is a boolean property through which you may choose whether the previous row selection will be cleared or not. The previous selection is preserved by default.
If you need to deselect all rows within a group programmatically, you can use the DeselectRowsInGroup
method.
Templating
Group Row Templates
The group row except for the expand/collapse UI is fully templatable. By default it renders a grouping icon and displays the field name and value it represents. The grouping record template is rendered against has the following signature:
As an example, the following template would make the group rows summary more verbose:
Blazor Grid Group By With Paging
Group rows participate in the paging process along with data rows. They count towards the page size for each page. Collapsed rows are not included in the paging process. Any expand or collapse operation forces Paging to recalculate the page count and adjust the page index if necessary. Groups that span multiple pages are split between them. The group row is visible only on the page it starts on and is not repeated on subsequent pages. Summary information for group rows is calculated based on the whole group and is unaffected by Paging.
Blazor Group By With Paging Example
Group By With Summaries
Integration between Group By and Summaries is described in the Summaries topic.
Keyboard Navigation
The grouping UI supports the following keyboard interactions:
For group rows (focus should be on the row or the expand/collapse cell)
- ALT + RIGHT - Expands the group
- ALT + LEFT - Collapses the group
- SPACE - selects all rows in the group, if rowSelection property is set to multiple
For group
IgbChip
components in the group by area (focus should be on the chip)- SHIFT + LEFT - moves the focused chip left, changing the grouping order, if possible
- SHIFT + RIGHT - moves the focused chip right, changing the grouping order, if possible
- SPACE - changes the sorting direction
- DELETE - ungroups the field
- The seperate elements of the chip are also focusable and can be interacted with using the ENTER key.
Blazor Grid Custom Group By
Grid allows defining custom grouping per column or per grouping expression, which provides grouping based on a custom condition. This is useful when you need to group by complex objects or for other application specific scenarios.
[!Note] In order to implement custom grouping the data first needs to be sorted appropriately. Due to this you may also need to apply a custom sorting strategy that extends the base
DefaultSortingStrategy
. After the data is sorted the custom groups can be determined by specifying aGroupingComparer
for the column or for the specific grouping expression.
The sample below demonstrates custom grouping by Date
, where the date values are sorted and grouped by Day, Week, Month or Year based on user-selected grouping mode.
Blazor Custom Group By Example
The sample defines custom sorting strategies for the different date conditions.
Each custom strategy extends the base DefaultSortingStrategy
and defines the CompareValues
method, which is the custom compare function used when sorting the values. Additionally it extracts the values from the date needed for the comparison.
A GroupingComparer
function is defined for the grouping expressions, which determines the items belonging to the same group based on the selected grouping mode. Values in the sorted data for which this function returns 0 are marked as part of the same group.
Known Limitations
Limitation | Description |
---|---|
Maximum amount of grouped columns is 10. | If more than 10 columns are grouped an error is thrown. |
API References
Additional Resources
- Grid overview
- Virtualization and Performance
- Paging
- Filtering
- Sorting
- Column Moving
- Summaries
- Column Resizing
- Selection
Our community is active and always welcoming to new ideas.