Skip to content

Row Grouping Plugin

The Row Grouping plugin organizes your rows into collapsible hierarchical groups. Perfect for organizing data by category, department, status, or any other dimension—or even multiple dimensions for nested grouping.

import '@toolbox-web/grid/features/grouping-rows';

The groupOn callback receives each row and should return an array representing the group path. For single-level grouping, return a one-element array. For multi-level grouping, return multiple elements (e.g., ['Region', 'Department']).

import { queryGrid } from '@toolbox-web/grid';
const grid = queryGrid('tbw-grid');
grid.gridConfig = {
columns: [
{ field: 'name', header: 'Employee' },
{ field: 'department', header: 'Department' },
{ field: 'salary', header: 'Salary', type: 'currency' }
],
features: {
groupingRows: {
groupOn: (row) => [row.department],
showRowCount: true,
defaultExpanded: false,
},
},
};
grid.rows = employees;
Animation Expand/collapse animation
Default expanded Expand groups by default
Show row count Display count of rows in group header
Accordion mode Only allow one group open at a time
Indent width (px) Indentation per nesting level

Pass a specific key string to defaultExpanded to expand only that group initially.

See GroupingRowsConfig for the full list of options and their defaults.

The defaultExpanded option controls which groups are expanded on initial render:

// Expand all groups
features: { groupingRows: { defaultExpanded: true, groupOn: ... } }
// Collapse all groups (default)
features: { groupingRows: { defaultExpanded: false, groupOn: ... } }
// Expand group at index 0
features: { groupingRows: { defaultExpanded: 0, groupOn: ... } }
// Expand group with specific key
features: { groupingRows: { defaultExpanded: 'Engineering', groupOn: ... } }
// Expand multiple groups by key
features: { groupingRows: { defaultExpanded: ['Engineering', 'Sales'], groupOn: ... } }

The animation option controls how grouped rows appear/disappear:

// Slide animation (default)
features: { groupingRows: { animation: 'slide', ... } }
// Fade animation
features: { groupingRows: { animation: 'fade', ... } }
// No animation
features: { groupingRows: { animation: false, ... } }

In accordion mode, only one group can be expanded at a time. Expanding a group automatically collapses all sibling groups at the same depth level.

features: {
groupingRows: {
groupOn: (row) => row.department,
accordion: true, // Only one group open at a time
},
},

Display aggregate values (sum, avg, count, min, max, first, last) in group headers. Works in both full-width and per-column rendering modes.

features: {
groupingRows: {
groupOn: (row) => row.department,
aggregators: {
salary: 'sum', // Sum of salaries in each group
bonus: 'avg', // Average bonus
id: 'count', // Count of rows
},
},
},

Built-in aggregators:

  • sum - Sum of numeric values
  • avg - Average of numeric values
  • count - Number of rows
  • min - Minimum value
  • max - Maximum value
  • first - First row’s value
  • last - Last row’s value

You can also provide a custom aggregator function:

aggregators: {
salary: (rows, field) => {
const total = rows.reduce((sum, r) => sum + r[field], 0);
return `$${total.toLocaleString()}`;
},
}

Return multiple values from groupOn to create nested group hierarchies:

features: {
groupingRows: {
groupOn: (row) => [row.region, row.department, row.team],
},
},

See GroupingRowsPlugin for the full list of methods.

const plugin = grid.getPluginByName('groupingRows');
plugin.expand('Engineering');
plugin.collapse('Engineering');
plugin.toggle('Engineering');
plugin.expandAll();
plugin.collapseAll();
plugin.isExpanded('Engineering'); // boolean
plugin.getExpandedGroups(); // string[]
plugin.setGroupOn((row) => [row.region, row.department]);
plugin.refreshGroups();

The row grouping plugin supports CSS custom properties for theming. Override these on tbw-grid or a parent container:

PropertyDefaultDescription
--tbw-group-indent-width1.25em (~20px)Indentation per group level
--tbw-grouping-rows-bgvar(--tbw-color-panel-bg)Group row background
--tbw-grouping-rows-bg-hovervar(--tbw-color-row-hover)Group row hover
--tbw-grouping-rows-toggle-hovervar(--tbw-color-row-hover)Toggle button hover
--tbw-grouping-rows-count-colorvar(--tbw-color-fg-muted)Count badge color
--tbw-grouping-rows-aggregate-colorvar(--tbw-color-fg-muted)Aggregate value color
--tbw-toggle-size1.25emToggle button size
--tbw-font-size-xs0.7857emCount text size
--tbw-animation-duration200msExpand/collapse animation
--tbw-animation-easingease-outAnimation curve
tbw-grid {
/* Custom row grouping styling */
--tbw-group-indent-width: 1.5em; /* Wider indentation */
--tbw-grouping-rows-bg: #e8f5e9;
--tbw-grouping-rows-bg-hover: #c8e6c9;
--tbw-grouping-rows-count-color: #388e3c;
}

The row grouping plugin uses these class names:

ClassElement
.group-rowGroup header row
.group-toggleExpand/collapse button
.group-labelGroup name and value
.group-countRow count badge
.group-aggregatesContainer for aggregate values
.group-aggregateIndividual aggregate value
.tbw-group-slide-inChild row slide animation
.tbw-group-fade-inChild row fade animation
[data-group-depth="N"]Group nesting level (0-4)

Click group headers to expand/collapse groups.

Event Log:
EventDetailDescription
group-toggle{ key, expanded, value, depth }Fired when a group is expanded/collapsed
AI assistants: For complete API documentation, implementation guides, and code examples for this library, see https://raw.githubusercontent.com/OysteinAmundsen/toolbox/main/llms-full.txt