# Drill through

> Open the raw rows behind a value cell in a DataGrid dialog with a column chooser and a row limit, edit them in any Data Grid editing mode, and write the edits back with incremental re-aggregation and onDataChange.

Canonical: https://salmonui.dev/docs/pivot-table/drill-through

```tsx
<PivotTable pivot={pivot} drillThrough />
<PivotTable pivot={pivot} drillThrough={{ maxRows: 500 }} editing={{ mode: 'dialog' }} />
```

With `drillThrough`, double-clicking a
value cell, or Enter on a focused one, opens a dialog with a `DataGrid` of
the raw rows aggregated into that cell. Subtotal and grand total cells
open every row below them. The grid has one column per visible field, a
column chooser, resizable columns and the row count; `maxRows` caps the
listed rows and the dialog says when the
list is truncated. `@salmonui/pivot-table/styles.css` carries the grid's
rules.

## Events

| Event | Payload |
| --- | --- |
| `onBeforeDrillThrough` | `{ rowKey, columnKey, measure, rowIndex, columnIndex, cell, rows(), source }`; `preventDefault()` keeps the dialog closed |
| `onDrillThrough` | the same, after the dialog opened |

`rows()` returns the raw rows on call, so a handler that only checks the
cell does not pay for the lookup. To show the rows in your own UI, cancel
the dialog and use them:

```tsx
<PivotTable
  data={data}
  defaultPivotModel={model}
  drillThrough
  onBeforeDrillThrough={(event) => {
    event.preventDefault();
    openSidePanel(event.rows());
  }}
/>
```

With a shared store (`pivot={pivot}`), these handlers are options of
`usePivot()`.

From code: `pivot.drillThrough(rowIndex, columnIndex)` opens the dialog
and `pivot.getCellRows(rowIndex, columnIndex)` returns the rows without it.

## Editing raw rows

`editing` is forwarded to the drill-through `DataGrid`, so every Data
Grid editing mode works there (`mode: 'row'`, `'dialog'`, `'batch'` and
`'cell'`), with
the add, edit, delete, save and cancel toolbar items:

```tsx
const [data, setData] = useState(initialRows);

<PivotTable
  data={data}
  defaultPivotModel={model}
  drillThrough
  editing={{ mode: 'batch' }}
  onDataChange={(event) => {
    setData(event.data);   // keep `data` in sync with the edited rows
    save(event.changes);   // [{ index, row, previous }]
  }}
/>
```

Saved edits write through to the pivot's raw rows: changed rows replace
the originals, added rows are appended and deleted rows removed. The pivot
re-aggregates (incrementally for a small number of changed rows), the
dialog keeps its list, and `onDataChange` receives `{ data, changes, action: 'update' | 'add' | 'delete' }`. Store
`event.data` as your `data` so a later render does not restore the old
rows. `pivot.updateRows(changes)` writes rows the same way from code.

With a [remote engine](https://salmonui.dev/docs/pivot-table/remote-pivot) the dialog loads
the first 1,000 raw rows of the cell from the server in one request and
lists up to `maxRows` of them; editing needs local `data`.

Drilling through from a chart point is covered with the
[pivot chart data](https://salmonui.dev/docs/pivot-table/chart).

### Live demo: Drill through with editing

Live demo: https://salmonui.dev/demos/pivot-table/pivot-table-drill-through

Double-click a value cell or press Enter on it to open its raw rows in a DataGrid dialog; edits, additions and deletions write through and re-aggregate the pivot.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { PivotTable, type PivotFieldDefinition, type PivotModelInput } from '@salmonui/pivot-table';

type Sale = { region: string; product: string; year: string; amount: number; units: number };

const REGIONS = ['Asia', 'Europe', 'Americas'];
const PRODUCTS = ['Bikes', 'Helmets', 'Gloves'];

function makeSales(): Sale[] {
  const rows: Sale[] = [];
  let n = 0;
  for (const region of REGIONS) {
    for (const product of PRODUCTS) {
      for (const year of ['2025', '2026']) {
        for (let k = 0; k < 4; k++) {
          n++;
          const amount = ((n * 173) % 700) + 100;
          rows.push({ region, product, year, amount, units: (amount % 23) + 1 });
        }
      }
    }
  }
  return rows;
}

const INITIAL = makeSales();

const FIELDS: PivotFieldDefinition<Sale>[] = [
  { field: 'region', caption: 'Region', type: 'string' },
  { field: 'product', caption: 'Product', type: 'string' },
  { field: 'year', caption: 'Year', type: 'string' },
  { field: 'amount', caption: 'Amount', type: 'number' },
  { field: 'units', caption: 'Units', type: 'number' },
];

const MODEL: PivotModelInput = {
  rows: [{ field: 'region' }, { field: 'product' }],
  columns: [{ field: 'year' }],
  values: [{ field: 'amount', aggregate: 'sum' }],
};

/** Double-click (or Enter on) a value cell to see its raw rows; edits write through and re-aggregate. */
export default function PivotTableDrillThroughDemo() {
  const [data, setData] = useState<readonly Sale[]>(INITIAL);
  const [status, setStatus] = useState(`${INITIAL.length} raw rows`);
  return (
    <div>
      <PivotTable<Sale>
        data={data}
        fields={FIELDS}
        defaultPivotModel={MODEL}
        locale="en-US"
        height={320}
        drillThrough={{ maxRows: 50 }}
        editing={{ mode: 'row' }}
        onDrillThrough={(event) => setStatus(`Drill through ${event.rowKey || 'Grand Total'} × ${event.columnKey || 'Grand Total'}: ${event.rows().length} rows`)}
        onDataChange={(event) => {
          setData(event.data);
          setStatus(`${event.action}: ${event.changes.length} row(s) written, ${event.data.length} raw rows`);
        }}
        aria-label="Sales with drill through"
      />
      <p className="demo-status" aria-live="polite">
        {status}
      </p>
    </div>
  );
}
```
