Components
Data Table
Powerful data tables built with @tanstack/react-table.
Loading...
Installation
Dependencies
npm install @tanstack/react-tableThe Data Table is built on top of the <Table /> component. See the Table documentation for installation.
Usage
"use client"
import {
type ColumnDef,
flexRender,
getCoreRowModel,
useReactTable,
} from "@tanstack/react-table"
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table"type Payment = {
id: string
amount: number
status: "pending" | "processing" | "success" | "failed"
email: string
}
const columns: ColumnDef<Payment>[] = [
{ accessorKey: "status", header: "Status" },
{ accessorKey: "email", header: "Email" },
{
accessorKey: "amount",
header: "Amount",
cell: ({ row }) => {
const formatted = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
}).format(row.getValue("amount"))
return <div className="text-right font-medium">{formatted}</div>
},
},
]export function DataTableDemo() {
const table = useReactTable({
data,
columns,
getCoreRowModel: getCoreRowModel(),
})
return (
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</TableHead>
))}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows.map((row) => (
<TableRow key={row.id}>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
</TableRow>
))}
</TableBody>
</Table>
)
}Examples
Cell Formatting
Use custom cell renderers in column definitions to format values and render components like <Badge />.
Loading...
Row Actions
Add a dropdown menu for row-level actions using row.original to access the row data.
Loading...
Sorting and Filtering
Add column sorting with getSortedRowModel and text-based filtering with getFilteredRowModel.
Loading...
Pagination, Selection and Column Visibility
Combine pagination, row selection with checkboxes, and column visibility toggling for a full-featured data table.
Loading...
API
See the @tanstack/react-table documentation for the full API reference.