Group
Enables rendering and handling of row groups. The actual grouping is done in the store, but triggered by shift + clicking headers, or by using the context menu, or by using two finger tap (one on header, one anywhere on grid). Use shift + alt + click, or the context menu, to remove a column grouper.
//<code-header>
fiddle.title = 'Group';
//</code-header>
const grid = new Grid({
appendTo : targetElement,
// makes grid as high as it needs to be to fit rows
autoHeight : true,
features : {
// group by food
group : 'food'
},
data : DataGenerator.generateData(5),
columns : [
{ field : 'name', text : 'Name', flex : 1 },
{ field : 'food', text : 'Favorite food', flex : 1 },
{ field : 'city', text : 'City', flex : 1 }
]
});Groups can be expanded/collapsed by clicking on the group row or pressing [space] when group row is selected. The actual grouping is done by the store, see group.
Grouping by a field performs sorting by the field automatically. It's not possible to prevent sorting. If you group, the records have to be sorted so that records in a group stick together. You can either control sorting direction, or provide a custom sorting function called groupSortFn to your feature config object.
For info on programmatically handling grouping, see StoreGroup.
Example snippets:
// grouping feature is enabled, no default value though
let grid = new Grid({
features : {
group : true
}
});
// use initial grouping
let grid = new Grid({
features : {
group : 'city'
}
});
// default grouper and custom renderer, which will be applied to each cell except the "group" cell
let grid = new Grid({
features : {
group : {
field : 'city',
ascending : false,
renderer : ({ isFirstColumn, count, groupRowFor, record }) => isFirstColumn ? `${groupRowFor} (${count})` : ''
}
}
});
// group using custom sort function
let grid = new Grid({
features : {
group : {
field : 'city',
groupSortFn : (a, b) => a.city.length < b.city.length ? -1 : 1
}
}
});
// can also be specified on the store
let grid = new Grid({
store : {
groupers : [
{ field : 'city', ascending : false }
]
}
});
// custom sorting function can also be specified on the store
let grid = new Grid({
store : {
groupers : [{
field : 'city',
fn : (recordA, recordB) => {
// apply custom logic, for example:
return recordA.city.length < recordB.city.length ? -1 : 1;
}
}]
}
});
Currently, grouping is not supported when using pagination, the underlying store cannot group data that is split into pages.
size param.
See the renderer config for details.Toggling a group collapsed state programmatically
You can easily toggle a group´s collapsed state, by passing a group member record like so:
// collapse the group for a certain record
grid.features.group.toggleCollapse(record, true);
Grouping by multi-value fields
The group field's value may be an array. This means that one record can be a member of more than one
group. When this is the case, the second and subsequent generated groups contain a
linked record which is a copy of the original record. Please note that some of
the linked records fields are not shared, like id (which is always generated).
//<code-header>
fiddle.title = 'Multi group';
//</code-header>
const grid = new Grid({
appendTo : targetElement,
// makes grid as high as it needs to be to fit rows
autoHeight : true,
features : {
// group by food
group : 'foods'
},
store : {
fields : [
'name',
{ name : 'foods', type : 'array' },
'city'
]
},
data : [
{
id : 1,
name : 'Bruce Wayne',
foods : ['Pizza', 'Sushi', 'Burgers'],
city : 'Gotham'
},
{
id : 2,
name : 'Clark Kent',
foods : ['Pizza', 'Burgers'],
city : 'Metropolis'
},
{
id : 3,
name : 'Barry Allen',
foods : ['Pizza', 'Sushi'],
city : 'Central City'
},
{
id : 4,
name : 'Diana Prince',
foods : ['Burgers', 'Sushi'],
city : 'Themyscira'
}
],
columns : [
{ field : 'name', text : 'Name', flex : 1 },
{
field : 'foods',
text : 'Favorite foods',
flex : 1,
renderer : ({ record, value }) => {
if (record.isGroupHeader) {
return value;
}
return value.join(', ');
}
},
{ field : 'city', text : 'City', flex : 1 }
]
});Keyboard shortcuts
This feature has the following default keyboard shortcuts:
| Keys | Action | Action description |
|---|---|---|
Space |
toggleGroup | When a group header is focused, this expands or collapses the grouped rows |
For more information on how to customize keyboard shortcuts, please see the Customizing keyboard shortcuts guide
This feature is enabled by default.
Configs
19
Configs
19Other
The sort direction of the groups.
The icon to use for the collapse icon in expanded state. Not applicable when using Scheduler in vertical mode.
The icon to use for the expand icon in collapsed state. Not applicable when using Scheduler in vertical mode.
The name of the record field to group by.
A function used to sort the groups. When grouping, the records have to be sorted so that records in a group stick together. Technically that means that records having the same field value should go next to each other. And this function (if provided) is responsible for applying such grouping order.
const grid = new Grid({
features : {
group : {
// group by category
field : 'category',
groupSortFn : (a, b) => {
const
aCategory = a.category || '',
bCategory = b.category || '';
// 1st sort by "calegory" field
return aCategory > bCategory ? -1 :
aCategory < bCategory ? 1 :
// inside calegory groups we sort by "name" field
(a.name > b.name ? -1 : 1);
}
}
}
});
| Parameter | Type | Description |
|---|---|---|
first | GroupRecord | The first group header record to compare |
second | GroupRecord | The second group header record to compare |
Returns 1 if first value is greater than second value, -1 if the opposite is true or 0 if they're equal
The height of group header rows. Can also be set on a per-group-header basis using the renderer. Not applicable when using Scheduler in vertical mode.
See Keyboard shortcuts for details. Not applicable when using Scheduler in vertical mode.
Set to true to show the number of members of each group in the group header. Not applicable when using
Scheduler in vertical mode.
By default, clicking anywhere in a group row toggles its expanded/collapsed state. Not applicable when using Scheduler in vertical mode.
Configure this as false to limit this to only toggling on click of the expanded/collapsed
state icon.
Rendering
A function which produces the HTML for a group header.
The function is called in the context of this Group feature object.
Default group renderer displays the groupRowFor and count.
| Parameter | Type | Description |
|---|---|---|
renderData | Object | Object containing renderer parameters |
renderData.groupRowFor | * | The value of the |
renderData.record | Model | The group record representing the group |
renderData.count | Number | Number of records in the group |
renderData.column | Column | The column the renderer runs for |
renderData.isFirstColumn | Boolean | True, if |
renderData.groupColumn | Column | The column under which the |
renderData.size | Object | Sizing information for the group header row, only |
renderData.size.height | Number | The height of the row, set this if you want a custom height for the group header row. That is UI part, so do not rely on its existence |
renderData.grid | Grid | The owning grid |
renderData.rowElement | HTMLElement | The owning row element |
Misc
Properties
20
Properties
20Common
Class hierarchy
Other
The sort direction of the groups.
The name of the record field to group by.
The height of group header rows. Can also be set on a per-group-header basis using the renderer. Not applicable when using Scheduler in vertical mode.
Set to true to show the number of members of each group in the group header. Not applicable when using
Scheduler in vertical mode.
By default, clicking anywhere in a group row toggles its expanded/collapsed state. Not applicable when using Scheduler in vertical mode.
Configure this as false to limit this to only toggling on click of the expanded/collapsed
state icon.
Functions
31
Functions
31Grouping
Collapse all groups. This function is exposed on Grid and can thus be called as grid.collapseAll()
Expand all groups. This function is exposed on Grid and can thus be called as grid.expandAll()
Other
Collapses or expands a group header record (you can also pass a record that is part of a group) depending on its current state.
| Parameter | Type | Description |
|---|---|---|
recordOrId | Model | String | Record or records id for a group row to collapse or expand |
collapse | Boolean | Force collapse ( |
Configuration
Events
Misc
Events
7
Events
7Fired when a group is going to be expanded or collapsed using the UI.
Returning false from a listener prevents the operation
// Adding a listener using the "on" method
group.on('beforeToggleGroup', ({ groupRecord, groupRecords, collapse, domEvent }) => {
});| Parameter | Type | Description |
|---|---|---|
groupRecord | Model | [DEPRECATED] Use |
groupRecords | Model[] | The group records being toggled |
collapse | Boolean | Collapsed (true) or expanded (false) |
domEvent | Event | The user interaction event (eg a |
Fired when one or more groups are expanded or collapsed
// Adding a listener using the "on" method
group.on('toggleGroup', ({ groupRecord, groupRecords, collapse, allRecords }) => {
});| Parameter | Type | Description |
|---|---|---|
groupRecord | Model | [DEPRECATED] Use |
groupRecords | Model[] | The group records being toggled |
collapse | Boolean | Collapsed (true) or expanded (false) |
allRecords | Boolean | True if this event is part of toggling all groups |
Event handlers
7
Event handlers
7Called when a group is going to be expanded or collapsed using the UI.
Returning false from a listener prevents the operation
new Group({
onBeforeToggleGroup({ groupRecord, groupRecords, collapse, domEvent }) {
}
});| Parameter | Type | Description |
|---|---|---|
groupRecord | Model | [DEPRECATED] Use |
groupRecords | Model[] | The group records being toggled |
collapse | Boolean | Collapsed (true) or expanded (false) |
domEvent | Event | The user interaction event (eg a |
Called when one or more groups are expanded or collapsed
new Group({
onToggleGroup({ groupRecord, groupRecords, collapse, allRecords }) {
}
});| Parameter | Type | Description |
|---|---|---|
groupRecord | Model | [DEPRECATED] Use |
groupRecords | Model[] | The group records being toggled |
collapse | Boolean | Collapsed (true) or expanded (false) |
allRecords | Boolean | True if this event is part of toggling all groups |
Typedefs
2
Typedefs
2A record representing a group header.
In addition to the groupChildren property, there is only one field defined in this record.
The name of the field is the same as the field used for grouping.
| Parameter | Type | Description |
|---|---|---|
groupChildren | Model[] | The records in the group |
CSS variables
13
CSS variables
13| Name | Description |
|---|---|
--b-group-header-font-weight | Group header font weight |
--b-group-header-border-width | Group header border width |
--b-group-header-zindex | Group header z-index |
--b-group-header-collapsing-zindex | Group header z-index during expand / collapse animation (to cover content moving underneath) |
--b-group-collapse-icon | Group collapse / expand icon (font icon char) |
--b-group-header-icon | Grouping indicator shown in the column header (font icon char) |
--b-group-header-background | Group header background |
--b-group-header-color | Group header color |
--b-group-header-icon-color | Group header icon color |
--b-group-header-border-color | Group header border color |
--b-group-header-stripe-background | Group header background when striping rows |
--b-group-count-badge-background | Group count badge background |
--b-group-count-badge-color | Group count badge color |