Skip to content

Master / Detail

Configure detail: { isMaster, renderer } and expand rows via setDetailExpanded(row, true) to mount nested grids or async detail panels beneath master rows without shifting parent row indices or page counts.

Master / Detail with Nested Child Grid
import { Grid, type ColumnDef } from 'next-grid-js';
import { createOrders, type OrderRow } from './data/orders';
import { syncGridTheme } from './theme';
import type { DemoMountContext } from './types';
import { addButton } from './ui';
export function mount({
stage,
controls,
meta,
}: DemoMountContext): () => void {
const rows = createOrders(15);
const columnDefs: readonly ColumnDef<OrderRow>[] = [
{
key: 'id',
name: 'ID',
width: 96,
},
{
key: 'customer',
name: 'Customer',
width: 175,
flex: 1,
},
{
key: 'product',
name: 'Product',
width: 175,
},
{
key: 'amount',
name: 'Amount',
width: 115,
cellDataType: 'number',
formatter: ({ value }) =>
`$${Number(value).toLocaleString('en-US')}`,
},
];
const grid = new Grid<OrderRow>({
container: stage,
columnDefs,
rowData: rows,
getRowId: (row) => row.id,
detail: {
isMaster: () => true,
estimatedHeight: 132,
renderer: ({ data, document: doc, registerChild }) => {
const wrap = doc.createElement('div');
wrap.style.cssText =
'padding:8px 14px;height:132px;box-sizing:border-box';
const host = doc.createElement('div');
host.style.cssText = 'height:100%;width:100%';
wrap.append(host);
const child = new Grid({
container: host,
ariaLabel: `Line items for ${data.id}`,
columnDefs: [
{
key: 'sku',
name: 'Item',
width: 180,
flex: 1,
},
{
key: 'qty',
name: 'Qty',
width: 85,
cellDataType: 'number',
},
{
key: 'unit',
name: 'Unit Price',
width: 110,
cellDataType: 'number',
},
],
rowData: [
{
sku: data.product,
qty: data.quantity,
unit: data.price,
},
{
sku: 'Handling & SLA',
qty: 1,
unit: 25,
},
],
getRowId: (r) => r.sku,
});
registerChild(child);
return {
element: wrap,
destroy: () => wrap.remove(),
};
},
},
});
const stopTheme = syncGridTheme(stage);
grid.setDetailExpanded({ rowId: rows[0].id }, true);
grid.addEventListener('detailChanged', ({ detail }) => {
meta.textContent = `Expanded details: ${detail.expanded.length}`;
});
addButton(controls, 'Toggle first row', () => {
const id = rows[0].id;
const isOpen = grid
.getDetailState()
.expanded.some((a) => 'rowId' in a && a.rowId === id);
grid.setDetailExpanded({ rowId: id }, !isOpen);
});
meta.textContent = 'Expanded details: 1';
return () => {
grid.destroy();
stopTheme();
};
}
  • isMaster(params): Must be cheap and side-effect-free (may run ahead of the visible viewport).
  • renderer(params): Receives { data, address, api, signal, registerChild } and may return MountedRowContent synchronously or via Promise.
  • registerChild(child): Register every nested Grid so the parent automatically destroys it when collapsed, recycled, or destroyed. Honor params.signal in async fetches.
  • State: getDetailState() and detailChanged expose expanded RealRowAddress entries, serialized in GridState v5.