Skip to content
Niko Table

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.

row-expansion.tsx
Order #
Customer
Items
Date
Total
Status
ORD-001
3 items
1/15/2024
$459.97
shipped
ORD-002
Sarah Johnson
3 items
1/16/2024
$249.48
processing
ORD-003
3 items
1/14/2024
$189.97
delivered
ORD-004
Emily Wilson
3 items
1/13/2024
$299.97
cancelled
Preview with Controlled State
row-expansion-state.tsx
Order #
Customer
Items
Date
Total
Status
ORD-001
3 items
1/15/2024
$459.97
shipped
ORD-002
Sarah Johnson
3 items
1/16/2024
$249.48
processing
ORD-003
3 items
1/14/2024
$189.97
delivered
ORD-004
Emily Wilson
3 items
1/13/2024
$299.97
cancelled
Row Expansion State
Live view of the row expansion table state with order details
Search Query:None
Total Orders:4
Expanded Orders:0
Items in Expanded:0
Expanded Orders Value:$0.00
Sorting:None
Page:1 (Size: 10)
Hidden Columns:0
View Full State Object
Expanded State:
{}
Expanded Orders Details:
[]
Sorting:
[]
Pagination:
{
  "pageIndex": 0,
  "pageSize": 10
}
Column Visibility:
{}

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.

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-sort

This example also uses card and separator from Shadcn UI:

pnpm dlx shadcn@latest add card separator

First 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.

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[]
}

Start with a table that has expandable rows.

First, define the columns, including an expand column.

columns.tsx
"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 component
function 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>
),
},
]

Next, create the table with row expansion on.

row-expansion-table.tsx
"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 component
function 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>
)
}

Row expansion works like this:

  1. Add a column with id: "expand", or set meta.expandedContent on any column
  2. Set enableExpanding: true in the config
  3. Provide expandedContent in the column’s meta

The expanded content comes from the column’s meta.expandedContent:

{
id: "expand",
meta: {
expandedContent: (order: Order) => <OrderDetails order={order} />,
},
}

Choose which rows can expand:

<DataTableRoot
data={data}
columns={columns}
config={{
enableExpanding: true,
}}
getRowCanExpand={row => row.original.items.length > 0}
>
{/* ... */}
</DataTableRoot>

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>
)
}

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.

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>
)
}

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>
)
}

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>
)
}
  1. 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.

  2. 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)[]}
  1. Export respects current filters. It follows all active filters, sorting, and pagination. Only the rows currently visible are exported.

✅ 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