Row Expansion Table
Add expandable rows that show more details inline.
Lets each row open an inline detail area behind a chevron toggle. It can hold any React: a sub-table, charts, key-value details. Use it when users jump often between row detail and the list. It is lighter than a side panel or modal, because nothing leaves the page.
Preview with Controlled State
Introduction
Section titled “Introduction”The Row Expansion Table lets users expand a row to see more details inline. It shows extra information without leaving the page or opening a modal.
This is not a Tree Table (nested getSubRows) or Grouping (column buckets). Expansion here opens an inline detail panel for one flat row.
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-pagination @niko-table/data-table-search-filter @niko-table/data-table-view-menu @niko-table/data-table-column-sortThis example also uses card and separator from Shadcn UI:
pnpm dlx shadcn@latest add card separatorFirst 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 orders with expandable items. Here’s what our data looks like:
type OrderItem = { id: string productName: string category: string price: number quantity: number}
type Order = { id: string orderNumber: string customer: string email: string total: number status: "pending" | "processing" | "shipped" | "delivered" | "cancelled" date: string items: OrderItem[]}Basic Table with Row Expansion
Section titled “Basic Table with Row Expansion”Start with a table that has expandable rows.
Column Definitions
Section titled “Column Definitions”First, define the columns, including an expand column.
"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 { ChevronRight, ChevronDown } from "lucide-react"
export type Order = { id: string orderNumber: string customer: string email: string total: number status: string date: string items: OrderItem[]}
// Expanded content componentfunction OrderDetails({ order }: { order: Order }) { return ( <div className="bg-muted/30 p-6"> <h3 className="font-semibold">Order Items</h3> <div className="space-y-2"> {order.items.map(item => ( <div key={item.id} className="flex justify-between"> <span>{item.productName}</span> <span>${item.price.toFixed(2)}</span> </div> ))} </div> </div> )}
export const columns: DataTableColumnDef<Order>[] = [ { id: "expand", header: () => null, cell: ({ row }) => { if (!row.getCanExpand()) return null return ( <Button variant="ghost" size="sm" onClick={row.getToggleExpandedHandler()} className="h-6 w-6 p-0" > {row.getIsExpanded() ? ( <ChevronDown className="h-3.5 w-3.5" /> ) : ( <ChevronRight className="h-3.5 w-3.5" /> )} </Button> ) }, size: 50, enableSorting: false, enableHiding: false, meta: { expandedContent: (order: Order) => <OrderDetails order={order} />, }, }, { accessorKey: "orderNumber", header: () => ( <DataTableColumnHeader> <DataTableColumnTitle title="Order #" /> <DataTableColumnSortMenu /> </DataTableColumnHeader> ), }, { accessorKey: "customer", header: () => ( <DataTableColumnHeader> <DataTableColumnTitle title="Customer" /> <DataTableColumnSortMenu /> </DataTableColumnHeader> ), }, { accessorKey: "total", header: () => ( <DataTableColumnHeader> <DataTableColumnTitle title="Total" /> <DataTableColumnSortMenu /> </DataTableColumnHeader> ), cell: ({ row }) => { const total = row.getValue("total") as number return <div className="font-mono">${total.toFixed(2)}</div> }, }, { accessorKey: "status", header: () => ( <DataTableColumnHeader> <DataTableColumnTitle title="Status" /> <DataTableColumnSortMenu /> </DataTableColumnHeader> ), },]<DataTable /> component
Section titled “<DataTable /> component”Next, create the table with row expansion on.
"use client"
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 } from "lucide-react"
type Order = { id: string orderNumber: string customer: string email: string total: number status: string date: string items: OrderItem[]}
// Expanded content componentfunction OrderDetails({ order }: { order: Order }) { return ( <div className="bg-muted/30 p-6"> <h3 className="font-semibold">Order Items</h3> <div className="space-y-2"> {order.items.map(item => ( <div key={item.id} className="flex justify-between"> <span>{item.productName}</span> <span>${item.price.toFixed(2)}</span> </div> ))} </div> </div> )}
const columns: DataTableColumnDef<Order>[] = [ { id: "expand", header: () => null, cell: ({ row }) => { if (!row.getCanExpand()) return null return ( <Button variant="ghost" size="sm" onClick={row.getToggleExpandedHandler()} className="h-6 w-6 p-0" > {row.getIsExpanded() ? ( <ChevronDown className="h-3.5 w-3.5" /> ) : ( <ChevronRight className="h-3.5 w-3.5" /> )} </Button> ) }, size: 50, enableSorting: false, enableHiding: false, meta: { expandedContent: (order: Order) => <OrderDetails order={order} />, }, }, { accessorKey: "orderNumber", header: () => ( <DataTableColumnHeader> <DataTableColumnTitle title="Order #" /> <DataTableColumnSortMenu /> </DataTableColumnHeader> ), }, { accessorKey: "customer", header: () => ( <DataTableColumnHeader> <DataTableColumnTitle title="Customer" /> <DataTableColumnSortMenu /> </DataTableColumnHeader> ), }, { accessorKey: "total", header: () => ( <DataTableColumnHeader> <DataTableColumnTitle title="Total" /> <DataTableColumnSortMenu /> </DataTableColumnHeader> ), cell: ({ row }) => { const total = row.getValue("total") as number return <div className="font-mono">${total.toFixed(2)}</div> }, }, { accessorKey: "status", header: () => ( <DataTableColumnHeader> <DataTableColumnTitle title="Status" /> <DataTableColumnSortMenu /> </DataTableColumnHeader> ), },]
export function RowExpansionTable({ data }: { data: Order[] }) { return ( <DataTableRoot data={data} columns={columns} config={{ enableExpanding: true, }} getRowCanExpand={row => row.original.items.length > 0} > <DataTableToolbarSection> <DataTableSearchFilter placeholder="Search orders..." /> <DataTableViewMenu /> </DataTableToolbarSection>
<DataTable> <DataTableHeader /> <DataTableBody> <DataTableEmptyBody /> </DataTableBody> </DataTable>
<DataTablePagination /> </DataTableRoot> )}How Row Expansion Works
Section titled “How Row Expansion Works”Row expansion works like this:
- Add a column with
id: "expand", or setmeta.expandedContenton any column - Set
enableExpanding: truein the config - Provide
expandedContentin the column’smeta
Expanded Content
Section titled “Expanded Content”The expanded content comes from the column’s meta.expandedContent:
{ id: "expand", meta: { expandedContent: (order: Order) => <OrderDetails order={order} />, },}Conditional Expansion
Section titled “Conditional Expansion”Choose which rows can expand:
<DataTableRoot data={data} columns={columns} config={{ enableExpanding: true, }} getRowCanExpand={row => row.original.items.length > 0}> {/* ... */}</DataTableRoot>Controlled Expansion State
Section titled “Controlled Expansion State”Control which rows are expanded yourself:
import { useState } from "react"import type { ExpandedState } from "@tanstack/react-table"
export function ControlledExpansionTable({ data }: { data: Order[] }) { const [expanded, setExpanded] = useState<ExpandedState>({})
return ( <DataTableRoot data={data} columns={columns} config={{ enableExpanding: true, }} state={{ expanded, }} onExpandedChange={setExpanded} > <DataTable> <DataTableHeader /> <DataTableBody> <DataTableEmptyBody /> </DataTableBody> </DataTable> </DataTableRoot> )}Exporting Data
Section titled “Exporting Data”You can export table data to CSV. For row expansion tables, keep these in mind:
- Expanded content is not exported. The CSV has only the main row data.
- Exclude the expand column. It is just a UI control.
- Export respects filters. Only visible, filtered rows are exported.
Using DataTableExportButton
Section titled “Using DataTableExportButton”The easiest way to add export:
import { DataTableExportButton } from "@/components/niko-table/components/data-table-export-button"
export function RowExpansionTableWithExport({ data }: { data: Order[] }) { return ( <DataTableRoot data={data} columns={columns} config={{ enableExpanding: true, }} getRowCanExpand={row => row.original.items.length > 0} > <DataTableToolbarSection> <DataTableSearchFilter placeholder="Search orders..." /> <DataTableExportButton filename="orders" excludeColumns={["expand"] as unknown as (keyof Order)[]} /> <DataTableViewMenu /> </DataTableToolbarSection>
<DataTable> <DataTableHeader /> <DataTableBody> <DataTableEmptyBody /> </DataTableBody> </DataTable>
<DataTablePagination /> </DataTableRoot> )}Using exportTableToCSV Directly
Section titled “Using exportTableToCSV Directly”For more control, call the exportTableToCSV function directly:
import { useDataTable } from "@/components/niko-table/core/data-table-context"import { exportTableToCSV } from "@/components/niko-table/filters/table-export-button"import { Button } from "@/components/ui/button"import { Download } from "lucide-react"
function ExportButton() { const { table } = useDataTable<Order>()
const handleExport = () => { exportTableToCSV(table, { filename: "orders", excludeColumns: ["expand"] as unknown as (keyof Order)[], }) }
return ( <Button size="sm" variant="outline" onClick={handleExport}> <Download className="mr-2 h-4 w-4" /> Export CSV </Button> )}
export function RowExpansionTableWithCustomExport({ data }: { data: Order[] }) { return ( <DataTableRoot data={data} columns={columns} config={{ enableExpanding: true, }} getRowCanExpand={row => row.original.items.length > 0} > <DataTableToolbarSection> <DataTableSearchFilter placeholder="Search orders..." /> <ExportButton /> <DataTableViewMenu /> </DataTableToolbarSection>
<DataTable> <DataTableHeader /> <DataTableBody> <DataTableEmptyBody /> </DataTableBody> </DataTable>
<DataTablePagination /> </DataTableRoot> )}Exporting Only Selected Rows
Section titled “Exporting Only Selected Rows”If row selection is on, you can export only the selected rows:
function ExportSelectedButton() { const { table } = useDataTable<Order>()
const handleExport = () => { exportTableToCSV(table, { filename: "selected-orders", excludeColumns: ["expand", "select"] as unknown as (keyof Order)[], onlySelected: true, }) }
const selectedCount = table.getFilteredSelectedRowModel().rows.length
if (selectedCount === 0) return null
return ( <Button size="sm" variant="outline" onClick={handleExport}> <Download className="mr-2 h-4 w-4" /> Export Selected ({selectedCount}) </Button> )}Important Notes
Section titled “Important Notes”-
Expanded content is not included. The CSV has only the main row data. Expanded content (like order items) is not exported. To export nested data, flatten it into extra columns or write a custom export function.
-
Always exclude the expand column. The expand column (
id: "expand") is only a UI control, so leave it out of exports:
excludeColumns={["expand"] as unknown as (keyof Order)[]}- Export respects current filters. It follows all active filters, sorting, and pagination. Only the rows currently visible are exported.
When to Use
Section titled “When to Use”✅ Use Row Expansion Table when:
- You need to show extra details for some rows
- You want to keep users in context (no navigation)
- The expanded content is directly tied to the row
- You prefer inline expansion over sidebars
❌ Consider other options when:
- The data is parent and child rows: use Tree Table
- You want to bucket flat rows by column values: use Grouping Table
- The expanded content is very large or always visible: use Aside Table
Next Steps
Section titled “Next Steps”- Aside Table: show details in a sidebar
- Tree Table: nested data with
getSubRows - Grouping Table: bucket flat rows by column values