# 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`.

```ts title="orders-grid.ts"
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,
});
```

**`ColumnGroupDef`** (interface from `@birchui/core`, full reference: https://docs.birchui.com/api/core.md)

```ts
interface ColumnGroupDef<TRow = RowData> extends Partial< Record<Exclude<keyof ColumnDef<TRow>, 'header' | 'headerClass'>, never> >
```

| Property | Type | Description |
| --- | --- | --- |
| `groupId?` | `string` |  |
| `header` | `string` |  |
| `children` | `(ColumnDef<TRow> \| ColumnGroupDef<TRow>)[]` |  |
| `headerClass?` | `string` |  |
| `collapsible?` | `boolean` |  |
| `openByDefault?` | `boolean` |  |
| `id?` | `undefined` |  |
| `field?` | `undefined` |  |
| `headerTooltip?` | `undefined` |  |
| `type?` | `undefined` |  |
| `width?` | `undefined` |  |
| `minWidth?` | `undefined` |  |
| `maxWidth?` | `undefined` |  |
| `flex?` | `undefined` |  |
| `pinned?` | `undefined` |  |
| `hidden?` | `undefined` |  |
| `sortable?` | `undefined` |  |
| `resizable?` | `undefined` |  |
| `reorderable?` | `undefined` |  |
| `rowHeader?` | `undefined` |  |
| `filter?` | `undefined` |  |
| `editable?` | `undefined` |  |
| `editor?` | `undefined` |  |
| `options?` | `undefined` |  |
| `valueGetter?` | `undefined` |  |
| `valueSetter?` | `undefined` |  |
| `valueParser?` | `undefined` |  |
| `format?` | `undefined` |  |
| `cell?` | `undefined` |  |
| `cellClass?` | `undefined` |  |
| `align?` | `undefined` |  |
| `comparator?` | `undefined` |  |
| `aggFunc?` | `undefined` |  |
| `rowGroup?` | `undefined` |  |
| `rowGroupIndex?` | `undefined` |  |
| `pivot?` | `undefined` |  |
| `pivotIndex?` | `undefined` |  |
| `meta?` | `undefined` |  |
| `columnGroupShow?` | `undefined` |  |
| `autoHeight?` | `undefined` |  |
| `wrapText?` | `undefined` |  |
| `rowDrag?` | `undefined` |  |
| `sparkline?` | `undefined` |  |

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 <kbd>Enter</kbd> or <kbd>Space</kbd> on the focused group header.

```ts
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.

**`ResolvedColumnGroup`** (interface from `@birchui/core`, full reference: https://docs.birchui.com/api/core.md)

```ts
interface ResolvedColumnGroup<TRow = RowData>
```

| Property | Type | Description |
| --- | --- | --- |
| `groupId` | `string` |  |
| `header` | `string` |  |
| `def` | `ColumnGroupDef<TRow>` |  |
| `depth` | `number` |  |
| `parentId` | `string \| null` |  |
| `collapsible` | `boolean` |  |
| `open` | `boolean` |  |
| `children` | `ColumnTreeItem<TRow>[]` |  |
| `columnIds` | `string[]` |  |
| `displayedColumnIds` | `string[]` |  |

## 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()` and `applyColumnState()` 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 `columnheader` cells with `aria-colspan`, and `aria-rowcount` and `aria-rowindex` include the extra header rows. <kbd>Arrow Up</kbd> and <kbd>Arrow Down</kbd> move between group headers and column headers.

## Related

- [Columns](https://docs.birchui.com/grid/columns.md) for every column option.
- [Column operations](https://docs.birchui.com/grid/column-operations.md) for moving, pinning and hiding columns.
- [Saving and restoring state](https://docs.birchui.com/grid/saving-state.md) to persist column layout.
