View as Markdown

Hierarchy Grid

Child grids

Hierarchy grids with child grids joined by a field, any nesting depth, inherited locale and theme, and lazy mounting.

childGrid renders a child DataGrid inside the detail row of each expanded parent. It takes the child rows in data and every DataGrid prop of the child; joinField names the field that relates parent and child rows, or parentField and childField when the names differ.

<DataGrid
  aria-label="Customers"
  data={customers}
  columns={customerColumns}
  childGrid={{
    data: orders,
    joinField: 'customerId',
    columns: orderColumns,
    childGrid: { data: items, parentField: 'id', childField: 'orderId', columns: itemColumns },
  }}
/>

Wrap a config in defineChildGrid<Child, Parent>(config) to type the child rows in its callbacks; an inline object sees them as unknown. A child grid may have its own childGrid, to any depth. An array is indexed once by the join value, so each child grid reads its rows in constant time; a remote data source receives the relation as a filter before the child grid's own filters, so the child grid cannot clear it. data may also be a function that receives the parent ({ row, rowId, depth }) and returns the child rows or a data source.

Expanding, focus and inheritance

Child grids expand and collapse with the detail row methods and events. They inherit locale, dir, timeZone and currency unless set, the parent's icons, and the theme through CSS. A child grid without aria-label or aria-labelledby is named by the childGridLabel localeText key. Keys pressed inside a child grid stay there; Tab moves in and out, Escape returns to the parent row. getChildGrid(rowId) returns the API of a mounted child grid.

Virtualization and export

A child grid mounts only when its detail row is rendered, so expanding every parent of a large grid creates only the visible child grids. Excel and PDF exports nest the child rows of expanded parents (hierarchy: 'expanded', the default), of every parent ('all') or none ('none'). Every nested level is written: Excel puts each level one column to the right and one outline level deeper (the sheet's outline depth follows the deepest level), PDF draws each level as an indented block under its row. Exports stop at 7 child levels, the Excel outline limit; deeper child grids are left out. Nested levels follow the child grid's own default expansion in 'expanded' mode. print() takes the same modes (printing.hierarchy or print({ hierarchy })) and prints each child grid as a nested table.

Child grids

Open demo

A three-level hierarchy grid (customers, orders, items) joined by field, with inherited locale, sortable child grids and lazy mounting.

Details
Customer
Country
Orders
Jun Park
Malaysia
196
Bao Nguyen
Malaysia
203
Dewi Lestari
Portugal
189
Goran Petrov
France
204
Hana Sato
Germany
194
Farah Aziz
Japan
200
Aiko Tan
Portugal
222
Ines Duarte
Portugal
184
Chen Wei
Japan
201
Elif Kaya
France
207

Child grids inherit the locale (switch it to see the amounts reformat) and mount only when their row renders.

API reference

DataGridChildGrid lists the childGrid options, DataGridChildGridParent the parent passed to a data function, and defineChildGrid types a config; getChildGrid and the detail methods are on DataGridApi.

Hierarchy print and export

Open demo

Print and Excel export of a hierarchy grid and of detail template rows with the hierarchy modes expanded, all and none.

Hierarchy grid: the first customer is expanded.

Details
Customer
Country
Jun Park
France
Order
Product
Amount
ORD-10000
Filter beans
$126.00
ORD-10021
Oolong tea
$48.45
Dewi Lestari
Germany
Bao Nguyen
Japan
Chen Wei
Japan
Goran Petrov
France
Ines Duarte
Germany
Hana Sato
France
Farah Aziz
France
Aiko Tan
Japan
Elif Kaya
Portugal

Detail template: the first two orders are expanded. The same detail hook writes the template text in print and export; expanded mode keeps the expanded orders only.

Details
Order
Customer
Amount
ORD-10000
Jun Park
$126.00

Filter beans × 10 to Reims, France

No note

ORD-10001
Dewi Lestari
$99.90

Green tea × 12 to Reims, France

No note

ORD-10002
Bao Nguyen
$55.17
ORD-10003
Chen Wei
$41.63
ORD-10004
Bao Nguyen
$100.80
ORD-10005
Goran Petrov
$125.80
ORD-10006
Goran Petrov
$108.38
ORD-10007
Ines Duarte
$661.98
No events yet.