Column groups
Nest columns under group headers to build multi-row headers, with collapsible groups that show a summary or the full detail.
Column groups put a header above related columns, such as Customer over name and region, or Billing over total, tax and currency. Groups nest to any depth, and each level adds one header row. They are part of the MIT grid in @birchui/core; no module is needed.
Defining groups#
Anywhere a column definition is accepted, columns and setColumns() also accept a ColumnGroupDef: an object with a header and children.
import { createGrid, type ColumnDef, type ColumnGroupDef } from '@birchui/core';
interface Order {
id: string;
customer: string;
region: string;
total: number;
tax: number;
currency: string;
}
const columns: (ColumnDef<Order> | ColumnGroupDef<Order>)[] = [
{ field: 'id', header: 'Order', pinned: 'left' },
{
groupId: 'customer',
header: 'Customer',
children: [{ field: 'customer', flex: 1 }, { field: 'region' }],
},
{
groupId: 'billing',
header: 'Billing',
collapsible: true,
children: [
{ field: 'total', type: 'number' },
{ field: 'tax', type: 'number', columnGroupShow: 'open' },
{ field: 'currency', columnGroupShow: 'open' },
],
},
];
const grid = createGrid<Order>(document.querySelector<HTMLElement>('#orders')!, {
columns,
rows: orders,
getRowId: (order) => order.id,
});A group cannot carry column settings such as field, width or sort; the type rejects them. Put those on the children, or use defaultColumn for settings shared by every column.
Collapsible groups#
Set collapsible: true and mark children with columnGroupShow:
columnGroupShow |
Column is visible |
|---|---|
'open' |
Only while the group is open |
'closed' |
Only while the group is closed |
| not set | Always |
Groups start closed unless openByDefault: true. columnGroupShow refers to the nearest collapsible group above the column. A chevron in the group header opens and closes it; keyboard users press Enter or Space on the focused group header.
grid.setColumnGroupOpen('billing', true);
grid.on('columnGroupOpened', ({ groupId, open }) => {
console.info(`${groupId} is now ${open ? 'open' : 'closed'}`);
});Reading the group tree#
getColumnGroups() returns the resolved groups in display order. A group without a groupId gets one from its position, such as group-1 or group-1-0, so give groups stable ids when you save their state or open them from code.
Behaviour#
- A group header spans its visible columns. When a group's columns are split between the pinned and scrolling areas, the header appears in each area.
- Dragging a column moves it only within its own group. Dragging a group header moves the whole group.
moveColumn()andapplyColumnState()keep groups together too. - Column stages from modules still receive the flat list of leaf columns, so grouping, pivoting and exports work unchanged.
- Group headers are
columnheadercells witharia-colspan, andaria-rowcountandaria-rowindexinclude the extra header rows. Arrow Up and Arrow Down move between group headers and column headers.
Related#
- Columns for every column option.
- Column operations for moving, pinning and hiding columns.
- Saving and restoring state to persist column layout.