# Stacked headers

> Group columns under multi-level headers with nested column definitions; operations act on the leaf columns.

Canonical: https://salmonui.dev/docs/data-grid/stacked-headers

A column with child `columns` is a column group: its header spans its
children and the header gains one row per level, with no depth limit.
Nested `<DataGridColumn>` children are the JSX form of the
same tree. Give every group an `id` so its state is keyed by a stable column
id.

```tsx
const columns = [
  { field: 'id', header: 'Order', pinned: 'start' },
  {
    id: 'customerGroup',
    header: 'Customer',
    columns: [
      { field: 'customer', header: 'Name' },
      { id: 'location', header: 'Location', columns: [{ field: 'country' }, { field: 'city' }] },
    ],
  },
];
```

## Leaf operations

Only leaf columns hold data, so sorting, filtering, resizing and the column
menu act on leaves; group header cells are not keyboard focus stops.
Reordering moves a leaf within its parent group and rejects a move
out of it, so groups never break apart.

Group headers take the same header options as leaves:
`headerAlign` (centered by default), `headerProps` for a class name, style,
title or `data-*` attributes, and `renderHeader` for custom content. Group
headers expose `aria-colspan`, and leaves under a shallower branch span the
remaining header rows with `aria-rowspan`.

## Pinning and virtualization

A group whose leaves end up in different pin zones is split at the zone
edge, one header cell per zone, with a development warning; pin whole
groups or top-level leaves to avoid it. Column virtualization stays
on under stacked headers: a group header spans the run of its rendered
leaves.

The column chooser lists the groups as an indented tree above their leaves.

### Live demo: Stacked headers

Live demo: https://salmonui.dev/demos/data-grid/stacked-headers

Three header levels from nested `columns`, with group `headerAlign`, `headerProps` and `renderHeader`; leaf sorting, filtering, resizing and reordering within the parent group; a pinned leaf beside the groups; and the column chooser listing the hierarchy.

`Demo.tsx`:

```tsx
'use client';

import { DataGrid, type DataGridColumn } from '@salmonui/data-grid';
import { EventLog, useEventLog } from '../../components/demo-kit';
import { createOrders, type Order } from '../../lib/fixtures';

const orders = createOrders(3_000, 5);

// Three header levels. Group columns carry a header, alignment, header props
// and an optional header template; sorting, filtering, resizing and
// reordering act on the leaves, and a leaf only moves within its parent.
const columns: DataGridColumn<Order>[] = [
  { field: 'id', header: 'Order', primaryKey: true, width: 110, pinned: 'start' },
  {
    id: 'customerGroup',
    header: 'Customer',
    headerAlign: 'start',
    columns: [
      { field: 'customer', header: 'Name', width: 150 },
      {
        id: 'location',
        header: 'Location',
        columns: [
          { field: 'country', header: 'Country', width: 120 },
          { field: 'city', header: 'City', width: 130 },
        ],
      },
    ],
  },
  {
    id: 'productGroup',
    header: 'Product',
    headerProps: { title: 'Product and category', 'data-group': 'product' },
    columns: [
      { field: 'product', header: 'Item', width: 150 },
      { field: 'category', header: 'Category', width: 120 },
    ],
  },
  {
    id: 'salesGroup',
    header: 'Sales',
    headerAlign: 'end',
    renderHeader: () => (
      <span>
        Sales <span className="demo-badge" data-tone="info">USD</span>
      </span>
    ),
    columns: [
      { field: 'quantity', header: 'Qty', type: 'number', align: 'end', width: 80 },
      { field: 'unitPrice', header: 'Unit price', type: 'number', format: 'C2', align: 'end', width: 110 },
      { field: 'discount', header: 'Discount', type: 'number', format: 'P0', align: 'end', width: 100 },
      { field: 'amount', header: 'Amount', type: 'number', format: 'C2', align: 'end', width: 120 },
    ],
  },
  { field: 'placed', header: 'Placed', type: 'datetime', format: 'yMMMd', width: 130 },
];

export default function StackedHeadersDemo() {
  const [log, push] = useEventLog();
  return (
    <>
      <DataGrid<Order>
        aria-label="Orders with stacked headers"
        data={orders}
        columns={columns}
        sorting
        filtering
        resizable
        reorderable
        toolbar={['columnChooser']}
        height={380}
        locale="en-US"
        currency="USD"
        timeZone="UTC"
        onColumnReorder={(event) => push(`reorder (${event.source}): ${event.field} ${event.fromIndex} → ${event.toIndex}`)}
        onBeforeColumnReorder={(event) => push(`before reorder: ${event.field} → ${event.toIndex}`)}
        onColumnResizeEnd={(event) => push(`resize (${event.source}): ${event.field} → ${Math.round(event.width)}`)}
        onSortChange={(event) => push(`sort (${event.source}): ${event.sortBy.map((s) => `${s.field} ${s.direction}`).join(', ') || 'cleared'}`)}
        onColumnVisibilityChange={(event) => push(`visibility (${event.source}): hidden ${event.hidden.join(', ') || '-'}; shown ${event.shown.join(', ') || '-'}`)}
      />
      <EventLog entries={log} />
    </>
  );
}
```

## API reference

[`DataGridColumn`](https://salmonui.dev/docs/reference/data-grid/DataGridColumn) (the
column type and the JSX component) and the core
[`ColumnDef`](https://salmonui.dev/docs/reference/data-grid/engine/ColumnDef) list the group
fields (`id`, `header`, `columns`, `headerAlign`, `headerProps`,
`renderHeader`).
