Tree Table
Show hierarchical data with expandable parent and child rows.
Shows nested rows (subRows) with collapsible parents and optional cascading selection. Checking a parent selects all its children, and a partly selected parent shows as indeterminate. Use it for parent and child data. It uses TanStack Table’s expanded-row model, so your column definitions need no recursive code.
Preview with Controlled State
Introduction
Section titled “Introduction”The Tree Table shows hierarchical data with parent and child rows. Users expand rows to see nested data, and selection can cascade to children.
Tree vs Grouping
Section titled “Tree vs Grouping”Both expand and collapse, but they are different features. Use one per table:
| Tree | Grouping Table | |
|---|---|---|
| Source | Nested data via getSubRows |
Flat data + column buckets |
| Who defines structure | Your data model | User / grouping state |
| Aggregations | Not the main model | aggregationFn / aggregatedCell |
| Typical UI | Indent + chevron per level | Group by in the column menu + (n) count |
Row Expansion is a third pattern: one flat row opens an inline detail panel. It has no nested rows and no column buckets.
Installation
Section titled “Installation”Install the DataTable core and add-ons for this example:
pnpm dlx shadcn@latest add @niko-table/data-table @niko-table/data-table-selection-bar @niko-table/data-table-pagination @niko-table/data-table-search-filter @niko-table/data-table-view-menu @niko-table/data-table-column-sortThis example also uses checkbox from Shadcn UI:
pnpm dlx shadcn@latest add checkboxFirst time using
@niko-table? See the Installation Guide to set up the registry.
For other add-ons or manual copy-paste, see the Installation Guide.
Prerequisites
Section titled “Prerequisites”We are going to build a table of projects with sub-projects. Here’s what our data looks like:
type Project = { id: string name: string status: "active" | "completed" | "on-hold" budget: number subRows?: Project[]}
const data: Project[] = [ { id: "1", name: "Website Redesign", status: "active", budget: 50000, subRows: [ { id: "1-1", name: "UI/UX Design", status: "completed", budget: 15000, }, { id: "1-2", name: "Frontend Development", status: "active", budget: 25000, }, ], }, // ...]Basic Tree Table
Section titled “Basic Tree Table”Start with a tree table.
Column Definitions
Section titled “Column Definitions”First, define the columns with a tree layout.
"use client"
import { DataTableColumnHeader } from "@/components/niko-table/components/data-table-column-header"import { DataTableColumnTitle } from "@/components/niko-table/components/data-table-column-title"import { DataTableColumnSortMenu } from "@/components/niko-table/components/data-table-column-sort"import type { DataTableColumnDef } from "@/components/niko-table/types"import { Button } from "@/components/ui/button"import { Checkbox } from "@/components/ui/checkbox"import { ChevronRight, ChevronDown } from "lucide-react"
export type Project = { id: string name: string status: "active" | "completed" | "on-hold" budget: number subRows?: Project[]}
export const columns: DataTableColumnDef<Project>[] = [ { accessorKey: "name", header: () => ( <DataTableColumnHeader> <DataTableColumnTitle /> <DataTableColumnSortMenu /> </DataTableColumnHeader> ), meta: { label: "Project Name" }, cell: ({ row }) => { const { depth, original: project } = row const canExpand = row.getCanExpand() const isExpanded = row.getIsExpanded()
return ( <div className="flex items-center gap-2"> {/* Tree visualization */} <div className="flex items-center"> {depth > 0 && ( <div className="flex h-6 w-6 items-center justify-center"> {/* Tree lines would go here */} </div> )} {canExpand && ( <Button variant="ghost" size="sm" onClick={row.getToggleExpandedHandler()} className="h-4 w-4 p-0" > {isExpanded ? ( <ChevronDown className="h-3.5 w-3.5" /> ) : ( <ChevronRight className="h-3.5 w-3.5" /> )} </Button> )} </div> <div className="font-medium">{row.getValue("name")}</div> </div> ) }, }, { accessorKey: "status", header: () => ( <DataTableColumnHeader> <DataTableColumnTitle /> <DataTableColumnSortMenu /> </DataTableColumnHeader> ), meta: { label: "Status" }, }, { accessorKey: "budget", header: () => ( <DataTableColumnHeader> <DataTableColumnTitle /> <DataTableColumnSortMenu /> </DataTableColumnHeader> ), meta: { label: "Budget" }, cell: ({ row }) => { const budget = row.getValue("budget") as number return <div className="font-mono">${budget.toLocaleString()}</div> }, },]<DataTable /> component
Section titled “<DataTable /> component”Next, create the tree table with getSubRows.
"use client"
import * as React from "react"import { DataTableRoot } from "@/components/niko-table/core/data-table-root"import { DataTable } from "@/components/niko-table/core/data-table"import { DataTableHeader, DataTableBody, DataTableEmptyBody,} from "@/components/niko-table/core/data-table-structure"import { DataTableToolbarSection } from "@/components/niko-table/components/data-table-toolbar-section"import { DataTablePagination } from "@/components/niko-table/components/data-table-pagination"import { DataTableSearchFilter } from "@/components/niko-table/components/data-table-search-filter"import { DataTableViewMenu } from "@/components/niko-table/components/data-table-view-menu"import { DataTableColumnHeader } from "@/components/niko-table/components/data-table-column-header"import { DataTableColumnTitle } from "@/components/niko-table/components/data-table-column-title"import { DataTableColumnSortMenu } from "@/components/niko-table/components/data-table-column-sort"import type { DataTableColumnDef } from "@/components/niko-table/types"import { Button } from "@/components/ui/button"import { ChevronRight, ChevronDown, ChevronsDownUp, ChevronsUpDown,} from "lucide-react"
type Project = { id: string name: string status: "active" | "completed" | "on-hold" budget: number subRows?: Project[]}
const columns: DataTableColumnDef<Project>[] = [ { accessorKey: "name", header: () => ( <DataTableColumnHeader> <DataTableColumnTitle /> <DataTableColumnSortMenu /> </DataTableColumnHeader> ), meta: { label: "Project Name" }, cell: ({ row }) => { const canExpand = row.getCanExpand() const isExpanded = row.getIsExpanded()
return ( <div className="flex items-center gap-2"> {canExpand && ( <Button variant="ghost" size="sm" onClick={row.getToggleExpandedHandler()} className="h-4 w-4 p-0" > {isExpanded ? ( <ChevronDown className="h-3.5 w-3.5" /> ) : ( <ChevronRight className="h-3.5 w-3.5" /> )} </Button> )} <div className="font-medium">{row.getValue("name")}</div> </div> ) }, }, { accessorKey: "status", header: () => ( <DataTableColumnHeader> <DataTableColumnTitle /> <DataTableColumnSortMenu /> </DataTableColumnHeader> ), meta: { label: "Status" }, }, { accessorKey: "budget", header: () => ( <DataTableColumnHeader> <DataTableColumnTitle /> <DataTableColumnSortMenu /> </DataTableColumnHeader> ), meta: { label: "Budget" }, cell: ({ row }) => { const budget = row.getValue("budget") as number return <div className="font-mono">${budget.toLocaleString()}</div> }, },]
export function TreeTable({ data }: { data: Project[] }) { const [expanded, setExpanded] = React.useState<Record<string, boolean>>({})
const expandAll = () => { const expandedRows: Record<string, boolean> = {} const expandProjects = (projects: Project[]) => { for (const project of projects) { if (project.subRows?.length) { expandedRows[project.id] = true expandProjects(project.subRows) } } } expandProjects(data) setExpanded(expandedRows) }
const collapseAll = () => { setExpanded({}) }
return ( <DataTableRoot data={data} columns={columns} config={{ enableExpanding: true, enableFilters: true, }} state={{ expanded, }} onExpandedChange={setExpanded} getSubRows={row => row.subRows} getRowCanExpand={row => Boolean(row.original.subRows?.length)} getRowId={row => row.id} > <DataTableToolbarSection> <DataTableSearchFilter placeholder="Search projects..." /> <div className="flex items-center gap-2"> <Button variant="outline" size="sm" onClick={expandAll}> <ChevronsDownUp className="mr-2 h-4 w-4" /> Expand All </Button> <Button variant="outline" size="sm" onClick={collapseAll}> <ChevronsUpDown className="mr-2 h-4 w-4" /> Collapse All </Button> <DataTableViewMenu /> </div> </DataTableToolbarSection>
<DataTable> <DataTableHeader /> <DataTableBody> <DataTableEmptyBody /> </DataTableBody> </DataTable>
<DataTablePagination /> </DataTableRoot> )}Tree Structure
Section titled “Tree Structure”getSubRows
Section titled “getSubRows”Tell TanStack Table how to find child rows:
<DataTableRoot data={data} columns={columns} getSubRows={row => row.subRows} getRowCanExpand={row => Boolean(row.original.subRows?.length)} getRowId={row => row.id}> {/* ... */}</DataTableRoot>Row Depth
Section titled “Row Depth”Read the row depth in cell renderers:
cell: ({ row }) => { const { depth } = row // depth: 0 = top level, 1 = first child, 2 = second level child, etc. return <div style={{ paddingLeft: `${depth * 20}px` }}>...</div>}Expand/Collapse All
Section titled “Expand/Collapse All”Control expansion state:
const [expanded, setExpanded] = useState<Record<string, boolean>>({})
const expandAll = () => { const expandedRows: Record<string, boolean> = {} const expandProjects = (projects: Project[]) => { for (const project of projects) { if (project.subRows?.length) { expandedRows[project.id] = true expandProjects(project.subRows) } } } expandProjects(data) setExpanded(expandedRows)}
const collapseAll = () => { setExpanded({})}Controlled State
Section titled “Controlled State”Manage all table state yourself:
import { useState } from "react"import type { ExpandedState, SortingState, ColumnFiltersState,} from "@tanstack/react-table"
export function ControlledTreeTable({ data }: { data: Project[] }) { const [expanded, setExpanded] = useState<ExpandedState>({}) const [sorting, setSorting] = useState<SortingState>([]) const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]) const [globalFilter, setGlobalFilter] = useState("")
const expandAll = () => { const expandedRows: Record<string, boolean> = {} const expandProjects = (projects: Project[]) => { for (const project of projects) { if (project.subRows?.length) { expandedRows[project.id] = true expandProjects(project.subRows) } } } expandProjects(data) setExpanded(expandedRows) }
const collapseAll = () => { setExpanded({}) }
return ( <DataTableRoot data={data} columns={columns} config={{ enableExpanding: true, enableFilters: true, }} state={{ expanded, sorting, columnFilters, globalFilter, }} onExpandedChange={setExpanded} onSortingChange={setSorting} onColumnFiltersChange={setColumnFilters} onGlobalFilterChange={setGlobalFilter} getSubRows={row => row.subRows} getRowCanExpand={row => Boolean(row.original.subRows?.length)} getRowId={row => row.id} > <DataTableToolbarSection> <DataTableSearchFilter placeholder="Search projects..." /> <div className="flex items-center gap-2"> <Button variant="outline" size="sm" onClick={expandAll}> <ChevronsDownUp className="mr-2 h-4 w-4" /> Expand All </Button> <Button variant="outline" size="sm" onClick={collapseAll}> <ChevronsUpDown className="mr-2 h-4 w-4" /> Collapse All </Button> <DataTableViewMenu /> </div> </DataTableToolbarSection>
<DataTable> <DataTableHeader /> <DataTableBody> <DataTableEmptyBody /> </DataTableBody> </DataTable>
<DataTablePagination /> </DataTableRoot> )}Tree Selection
Section titled “Tree Selection”For hierarchical selection (selecting a parent selects all children), you need custom selection logic. The live demo at the top of this page has it. Open its Code tab to see the cascading-selection column.
When to Use
Section titled “When to Use”✅ Use Tree Table when:
- You have hierarchical data (parent and child relationships)
- You need to show nested structures
- Users need to expand and collapse sections
- The data has several levels of nesting
❌ Don’t use Tree Table when:
- The data is flat and you want to bucket by column values: use Grouping Table
- You need per-row detail panels: use Row Expansion
- You prefer sidebars for details: use Aside Table
Next Steps
Section titled “Next Steps”- Grouping Table: bucket flat rows by column values
- Row Expansion Table: inline detail panels
- Advanced Table: combine with other features