developer guide
Tree data and master-detail: JavaScript Data Grid Guide
A hierarchy from a parent reference or a path, orphans and cycles absorbed rather than dropped, lazy children, and rows that expand into a nested grid.
Developer guide › Tree data and master-detail
Tree data
Rows that sit under one another rather than in a flat list. Two shapes, and they answer different questions about where the hierarchy lives.
The row names its parent
tree: {
parentKey: 'parentId', // a field, or a function of the row
label: 'name', // what the tree column shows
orphans: 'Unassigned', // or 'root', the default
}
The row carries its own ancestry
tree: { path: (row) => row.hierarchy } // ['EMEA', 'UK', 'Colchester']
Parent-reference is what a join or a document store produces. Every node is a real row. A row whose parent is not in the data (filtered away, not loaded, or simply wrong) is an orphan: it goes to the root by default, or into a named bucket. It is never dropped, because hiding a record over a bad reference loses data the user can see in a flat view.
Path-based rows describe their own place, so intermediate levels may have no
row at all: EMEA/UK/Colchester with no EMEA/UK row still needs a
UK node to sit under. Those are synthesised, and render as group rows: a heading
over the rows beneath it with no record of its own. A real row arriving later for a level
already synthesised fills that node rather than appearing beside it.
A parent cycle is not a hierarchy and cannot be walked. It is reported once and cut, with the rows shown at the root: wrong place beats vanished.
The grid generates a tree column to carry the expander and the indent, on the
same terms as the auto-group, selection and detail columns: pinned to the start, and absent
from columns.visible(), saved views, exports and the tool panel. Its text comes
from tree.label; without one it falls back to your first visible column, which
then appears twice until you hide it, the grid does not remove a column you did not ask it
to remove.
Loading a branch on demand
Children fetched when the node is opened
tree: {
parentKey: 'parentId',
hasChildren: (data) => data.childCount > 0,
loadChildren: (row, signal) => api.children(row.data.id, { signal }),
}
hasChildren lets a row declare children it does not hold, so the expander is
there before anything is fetched: without it there is nothing to click and the branch can
never load. Such a node reads as closed even though tree nodes are otherwise expanded
by default, because an open branch with nothing under it leaves no gesture to load it.
The rows that arrive are added to the data set, so the hierarchy rebuilds through the same
pipeline as everything else and the new rows sort, filter and export like any other. A branch
is fetched once however often it is toggled; closing it before the rows arrive aborts the
request through the signal. A rejection is reported and leaves the branch
unloaded, so reopening tries again rather than showing an empty node for good.
tree:loading, tree:loaded and tree:loadFailed are on the
event bus.
Expansion is the same state group expansion uses, so rows.expand,
rows.collapse, expandAll, collapseAll and saved views
all work on it. A collapsed branch is skipped rather than hidden, so it costs nothing.
Grouping and tree together is not a combination: the grouping wins and says so
once, because two expanders in one row would be two hierarchies claiming the same rows.
Drag to reparent
rowReorder: true over a field-form tree.parentKey (a plain field
name, not a function and not a path tree) gains a third drop position alongside
the ordinary before/after of row reorder: dropping on the
upper quarter of a row places the dragged row immediately before it, as a new
sibling; the lower quarter places it immediately after, also as a sibling; and
the middle half places it inside that row, as its new last child. The
same split the Gantt's own row drag already uses, so the gesture reads the same wherever it is
dropped. A row cannot be dropped into one of its own descendants (nor onto itself) - doing
so would make a row its own ancestor, which the hierarchy has no way to render, and the drop is
refused outright, before any data changes, and announced.
A plain grid's tree gains the third drop position for free
createGrid(element, {
columns,
rows,
tree: { parentKey: 'parentId', label: 'name' },
rowReorder: true,
});
grid.on('beforeRowDrop', ({ key, parent, index, position, target, preventDefault }) => {
// position is 'before' | 'after' | 'inside'; veto with preventDefault(reason?)
if (position === 'inside' && isLocked(target)) preventDefault('locked');
});
grid.on('rowDrop', ({ key, parent, index, position, target }) => {
api.reparent(key, parent);
});
Two grid primitives carry it out, not a third drag implementation. A drop
writes the row's parentKey field through rows.apply({ update }) and
then carries it to its new physical slot through rows.move, both inside the same
beforeRowDrop/rowDrop:cancelled/rowDrop gate a cross-grid
row transfer already uses: a vetoed drop writes neither, so a row
can never end up attached to a parent it was refused a position under. beforeRowDrop
is vetoable with preventDefault(reason?) (or returning false);
rowDrop:cancelled follows a veto or a drop that went stale while an async handler
was thinking, and carries the same shape plus reason; rowDrop follows
once both writes have committed. Every one of the three carries key (the row
dropped), parent (its parentKey value after the drop),
index (the dropped row's final 0-based position among its new siblings, not a
physical row index), position and target (the row it was dropped
onto). A drop in the middle half lands as the new parent's last child by default; tree:
{ insidePosition: 'first' } lands it first instead.
This is the standalone tree module's own drag-to-reparent, exposed on
a plain grid tree too: createTree(el, config)'s dragMode is this
same rowReorder, turned on, over the module's own generated
tree.parentKey, and its insidePosition is the same
tree.insidePosition above. A path-form or function-form
tree.parentKey has no single field a drop could write a new parent into, so such
a tree keeps today's before/after-only drag rather than being offered a middle position it
cannot carry out. The module reads the same two data shapes as a plain grid's own
tree option - flat parentKey rows, or nested items
whose own childrenField array (not children on a plain grid row,
which the grid does not read) it flattens once on construction - and draws each node's
text from labelField or a template function. Its own
dragHandle ('grip', the default; 'row', matching the
list view's own; or false) says where the drag starts from, independently of
dragMode.
A tree whose drag starts anywhere on the row, and drops inside a node as its new first child
createTree(element, {
data: rows,
dragMode: 'both',
dragHandle: 'row',
insidePosition: 'first',
});
// a plain grid's own tree config takes the same option
createGrid(element, {
columns, rows,
tree: { parentKey: 'parentId', insidePosition: 'first' },
rowReorder: { handle: 'row' },
});
Master-detail
A master row expands to reveal a detail region: by default a nested grid over whatever
detail.rows(row) returns, which may be a promise. The grid adds an expander
column while the feature is on, on the same terms as the auto-group and selection columns:
pinned to the start, and absent from columns.visible(), saved views, exports and
the tool panel.