Demo
Drill through with editing
Documented in Drill through.
Drill through with editing
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.
72 raw rows
Demo.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>
);
}
The dialog lists at most 50 rows (drillThrough={{ maxRows: 50 }}) and has
a column chooser. Edit an amount and save: onDataChange returns the new
raw rows, the demo stores them as its data, and the pivot re-aggregates
without a full recompute.