Introducing PrimeReact v11-alpha 🎉Discover Now
styledData

TreeTable

TreeTable renders hierarchical data with the same feature set as DataTable — sort, filter, pagination, selection, scroll, frozen, resize, reorder, editing, export.

NameSizeType
Documents
75kbFolder
preview

Usage#

import { DataTable } from '@primereact/ui/datatable';

TreeTable is DataTable with the treeMode prop set on DataTable.Root. Every feature shown in DataTable continues to work over the flattened tree — the only structural difference is that the first column wraps its cell content with DataTable.RowToggle to render the expand/collapse chevron.

<DataTable.Root data={treeNodes} dataKey="key" treeMode expandedKeys={expandedKeys} onExpandedChange={(e) => setExpandedKeys(e.value)}>
    <DataTable.TableContainer>
        <DataTable.Table>
            <DataTable.THead>
                <DataTable.THeadRow>
                    <DataTable.THeadCell>Name</DataTable.THeadCell>
                </DataTable.THeadRow>
            </DataTable.THead>
            <DataTable.TBody>
                {({ item }) => (
                    <DataTable.Row>
                        <DataTable.Cell>
                            <DataTable.RowToggle />
                            {item.name}
                        </DataTable.Cell>
                    </DataTable.Row>
                )}
            </DataTable.TBody>
        </DataTable.Table>
    </DataTable.TableContainer>
</DataTable.Root>

Data shape#

Each node carries its own key, arbitrary data, and an optional children array:

type TreeNode = {
    key: string;
    data: Record<string, unknown>;
    children?: TreeNode[];
};

Set dataKey="key" and resolve column values against item.<field> — the component flattens data onto the row so columns can read directly.

Rows are augmented with derived fields the component consumes internally: _treeLevel, _treeHasChildren, _treePosInSet, _treeSetSize. These drive the ARIA attributes (aria-level, aria-expanded, aria-posinset, aria-setsize) automatically.

Examples#

Basic#

Displays hierarchical nodes with expand/collapse chevrons on the first column.

basic-demo

Size#

Use the size prop with small or large to adjust cell padding.

size-demo

Gridlines#

Enable the showGridlines prop to render borders between cells.

gridlines-demo

Striped Rows#

The stripedRows prop renders rows with alternating background colors across the flattened tree.

striped-rows-demo

Selection#

Selection works exactly like DataTable — selectionMode controls the mode, and the DataTable.Selection render prop exposes helpers for row-level and header-level toggles. Checkbox selection propagates across the hierarchy with tri-state semantics.

Single#

single-selection-demo

Multiple#

Pair selectionMode="multiple" with metaKeySelection so Ctrl/Cmd + Click toggles rows and Shift + Click selects a range.

multiple-selection-demo

Checkbox#

Checkbox column with propagating selection — toggling a parent selects all descendants, partial state renders as indeterminate.

checkbox-selection-demo

Radio#

radio-selection-demo

Keyboard#

Arrow Up/Down moves focus between rows, Arrow Right/Left expands/collapses, Space or Enter toggles the focused row, and Shift + Arrow extends a range.

keyboard-demo

Sort#

Sorting is evaluated per tree level — siblings are sorted against each other while the hierarchy stays intact.

Single#

sort-demo

Multiple#

multisort-demo

Presort#

presort-demo
presort-multi-demo

Pagination#

Pagination operates on root-level nodes — expanded children never get paginated away.

pagination-demo

Scroll#

scrollable + scrollHeight works the same way as in DataTable; the flattened rows scroll while the sticky header stays pinned.

Vertical#

scroll-vertical-demo

Horizontal#

scroll-horizontal-demo

Flex#

scroll-flex-demo

Frozen Columns#

Pin the tree column (or any column) during horizontal scroll with the frozen prop on DataTable.Column.

scroll-frozen-columns-demo

Multiple columns can be frozen on both sides — pass alignFrozen="right" to pin to the right edge.

scroll-frozen-columns-multi-demo

Frozen Rows#

Keep specific nodes pinned above the scrollable body by rendering them inside DataTable.FrozenTBody.

scroll-frozen-rows-demo

Editing#

Both editMode="cell" and editMode="row" compose with treeMode. Editing state is keyed by the row key, so collapsing or expanding other branches never drops edits in flight.

Cell#

cell-edit-demo

Cell editing composes with row selection.

cell-edit-selection-demo

Row#

row-edit-demo

Column Resize#

Resize works in tree mode via the resizableColumns prop with columnResizeMode (fit or expand).

Fit Mode#

resize-demo

Expand Mode#

resize-expand-demo

Column Reorder#

Drag and drop column headers to reorder columns.

reorder-demo

Row Reorder#

Root-level row reordering via the drag handle on DataTable.RowReorder. Children stay attached to their parents.

row-reorder-demo

Column Toggle#

Show/hide columns and drag to reorder them in a popover.

column-toggle-demo

Column Group#

Multi-level headers with rowSpan and colSpan, plus an aggregate TFoot.

column-group-demo

Sort and filter work on any leaf header cell in a grouped layout — wrap the header in DataTable.Sort and add a filter row below the leaf row.

column-group-filter-sort-demo

Filter#

Filtering uses the same filters + onFilter API as DataTable; treeMode honours it against the flattened tree. For the standalone hook used outside DataTable, see useTreeFilter.

Basic#

display="row" renders inline inputs directly under each column header and applies as the user types.

filter-basic-demo

Advanced#

display="menu" swaps the inline input for a trigger icon that opens a popover with multiple constraints, match mode, operator, and Apply/Clear buttons.

filter-advanced-demo

Export#

Only expanded (visible) rows are exported to CSV.

export-demo

Lazy Loading#

Fetch each branch on demand when the user expands it.

lazy-demo

Loading#

Overlay#

loading-overlay-demo

Skeleton#

loading-skeleton-demo

Empty State#

Use EmptyTBody for a custom empty-state view.

empty-demo

Advanced#

Sort, filter (per-column + global), and cell editing composed in a single tree.

advanced-demo

Theming#

TreeTable inherits every theme token, CSS class, and pass-through target from the DataTable theme. The parts you'll most likely customise when theming TreeTable:

  • DataTable.RowToggle — the chevron button on the first column (data-part="row-toggle").
  • DataTable.RowToggleIndicator — the icon slot, matched by match="expanded" and match="collapsed".
  • Row-level _treeLevel drives the default left padding via CSS. Override per-level spacing by styling on the aria-level="<n+1>" attribute rendered on <tr>.

Sub-Components#

See DataTable Primitive for the full sub-component API — TreeTable uses the same primitives.

Hooks#

See useDataTable for the headless hook API.

Accessibility#

See DataTable Primitive for WAI-ARIA compliance details and keyboard support. Tree-specific ARIA attributes (aria-level, aria-expanded, aria-posinset, aria-setsize) are wired automatically.