A data table on a real <table>, rendered on the server: cells, badges and formatted numbers never reach the client bundle. A small client controller adds sorting, search and pagination by moving and hiding the existing rows. Row selection is native checkboxes: the highlight is pure CSS through :has(:checked), and inside a form the selected ids are submitted like any other field. The header and the first column stay pinned while you scroll.
Browser Support
Limited availability5 featuresDetails
Highlights selected rows and shows the bulk actions. Without it selection still works, rows are just not tinted.
Fades in the shadow of the pinned column when you scroll sideways. Without it a static border marks the edge.
Hides the pager, search and select-all when JavaScript is off, and shows every row instead of the first page.
Mixes the zebra, hover and selection tints.
Styles the open list of the rows-per-page select; other browsers keep the native list.
Preview
Sort by any header, search by client or status, select a few rows: the toolbar appears and counts them.
| INV-2041 | Northwind Traders ap@northwind.com | Paid | Dec 20 | $240 | |
|---|---|---|---|---|---|
| INV-2040 | Orbit Analytics ap@orbitanalytics.ai | Draft | Dec 16 | $8,159 | |
| INV-2039 | Bramble Coffee Roasters hello@bramblecoffee.co | Overdue | Dec 13 | $7,079 | |
| INV-2038 | Lumen Dental Group billing@lumendental.com | Paid | Dec 9 | $5,997 | |
| INV-2037 | Marigold Events team@marigoldevents.com | Pending | Dec 6 | $4,917 | |
| INV-2036 | Atlas Physio admin@atlasphysio.com | Paid | Dec 3 | $3,837 | |
| INV-2035 | Harbor & Pine Architects finance@harborpine.com | Pending | Nov 29 | $2,755 | |
| INV-2034 | Stonebridge Schools procurement@stonebridge.edu | Paid | Nov 26 | $1,675 | |
| INV-2033 | Greyfield Legal LLP accounts@greyfield.law | Draft | Nov 22 | $593 | |
| INV-2032 | Kestrel Logistics invoices@kestrel.io | Overdue | Nov 19 | $8,511 | |
| INV-2031 | Northwind Traders ap@northwind.com | Paid | Nov 16 | $7,431 | |
| INV-2030 | Orbit Analytics ap@orbitanalytics.ai | Pending | Nov 12 | $6,349 | |
| INV-2029 | Bramble Coffee Roasters hello@bramblecoffee.co | Paid | Nov 9 | $5,269 | |
| INV-2028 | Lumen Dental Group billing@lumendental.com | Pending | Nov 5 | $4,189 | |
| INV-2027 | Marigold Events team@marigoldevents.com | Paid | Nov 2 | $3,107 | |
| INV-2026 | Atlas Physio admin@atlasphysio.com | Draft | Oct 30 | $2,027 | |
| INV-2025 | Harbor & Pine Architects finance@harborpine.com | Overdue | Oct 26 | $945 | |
| INV-2024 | Stonebridge Schools procurement@stonebridge.edu | Paid | Oct 23 | $8,863 | |
| INV-2023 | Greyfield Legal LLP accounts@greyfield.law | Pending | Oct 19 | $7,783 | |
| INV-2022 | Kestrel Logistics invoices@kestrel.io | Paid | Oct 16 | $6,701 | |
| INV-2021 | Northwind Traders ap@northwind.com | Pending | Oct 13 | $5,621 | |
| INV-2020 | Orbit Analytics ap@orbitanalytics.ai | Paid | Oct 9 | $4,541 | |
| INV-2019 | Bramble Coffee Roasters hello@bramblecoffee.co | Draft | Oct 6 | $3,459 | |
| INV-2018 | Lumen Dental Group billing@lumendental.com | Overdue | Oct 2 | $2,379 | |
| INV-2017 | Marigold Events team@marigoldevents.com | Paid | Sep 29 | $1,297 | |
| INV-2016 | Atlas Physio admin@atlasphysio.com | Pending | Sep 26 | $9,215 | |
| INV-2015 | Harbor & Pine Architects finance@harborpine.com | Paid | Sep 22 | $8,135 | |
| INV-2014 | Stonebridge Schools procurement@stonebridge.edu | Pending | Sep 19 | $7,055 | |
| INV-2013 | Greyfield Legal LLP accounts@greyfield.law | Paid | Sep 15 | $5,973 | |
| INV-2012 | Kestrel Logistics invoices@kestrel.io | Draft | Sep 12 | $4,893 | |
| INV-2011 | Northwind Traders ap@northwind.com | Overdue | Sep 9 | $3,811 | |
| INV-2010 | Orbit Analytics ap@orbitanalytics.ai | Paid | Sep 5 | $2,729 | |
| INV-2009 | Bramble Coffee Roasters hello@bramblecoffee.co | Pending | Sep 2 | $1,649 | |
| INV-2008 | Lumen Dental Group billing@lumendental.com | Paid | Aug 29 | $567 | |
| INV-2007 | Marigold Events team@marigoldevents.com | Pending | Aug 26 | $8,487 | |
| INV-2006 | Atlas Physio admin@atlasphysio.com | Paid | Aug 23 | $7,407 | |
| INV-2005 | Harbor & Pine Architects finance@harborpine.com | Draft | Aug 19 | $6,325 | |
| No invoices match your search. | |||||
Installation
Usage
import { DataTable, type DataTableColumn } from "@/components/ui/data-table"
type Invoice = {
number: string; client: string; amount: number; due: Date
status: "paid" | "pending" | "overdue" | "draft"
}
const money = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" })
const year = new Date().getUTCFullYear()
const formatDay = (d: Date) =>
new Intl.DateTimeFormat("en-US", {
month: "short", day: "numeric",
year: d.getUTCFullYear() === year ? undefined : "numeric",
}).format(d)
const tones = { paid: "success", pending: "warning", overdue: "danger", draft: "muted" } as const
const columns: DataTableColumn<Invoice>[] = [
{ id: "number", header: "Invoice", accessor: (r) => r.number, sortable: true,
numeric: true, align: "left" },
{ id: "client", header: "Client", accessor: (r) => r.client, sortable: true },
{ id: "status", header: "Status", accessor: (r) => r.status, sortable: true,
// Quiet status: a coloured dot plus plain text, red only for the one that needs action
cell: (r) => <Badge variant="status" dot tone={tones[r.status]}>{r.status}</Badge> },
{ id: "due", header: "Due date", accessor: (r) => r.due, sortable: true,
numeric: true, align: "left",
// "Nov 16" this year, "Nov 16, 2025" otherwise
cell: (r) => formatDay(r.due) },
{ id: "amount", header: "Amount", accessor: (r) => r.amount, sortable: true,
numeric: true, cell: (r) => money.format(r.amount) },
]
// A Server Component: columns with cell functions stay on the server
export default async function Invoices() {
const invoices = await getInvoices()
return (
<DataTable
caption="Invoices"
columns={columns}
data={invoices}
getRowId={(r) => r.number}
defaultSorting={{ columnId: "due", direction: "desc" }}
searchable
pageSize={10}
/>
)
}Examples
Selection in a form
Every row checkbox is <input name="selected" value={id}>. Put the table in a form and a Server Action receives the ids; the bulk buttons are ordinary submit buttons.
async function deleteInvoices(formData: FormData) {
"use server"
const ids = formData.getAll("selected") // ["INV-2041", "INV-2038"]
await db.invoice.deleteMany({ where: { number: { in: ids } } })
revalidatePath("/invoices")
}
<form action={deleteInvoices}>
<DataTable
selectable
columns={columns}
data={invoices}
getRowId={(r) => r.number}
toolbar={
<div data-selection-only className="ml-auto flex items-center gap-2">
<span><b data-selected-count>0</b> selected</span>
<button type="submit">Delete</button>
<button type="button" data-table-clear-selection>Clear</button>
</div>
}
/>
</form>Selection callback and loading
Rendered from a client component, the table accepts onSelectionChange, onSortingChange and onPageChange. Here the selection sums the amounts, and the button toggles the skeleton rows.
Select rows to see the total
| INV-2041 | Northwind Traders | Nov 20 | $240.00 | |
|---|---|---|---|---|
| INV-2040 | Orbit Analytics | Nov 16 | $8,159.13 | |
| INV-2039 | Bramble Coffee Roasters | Nov 13 | $7,079.26 | |
| INV-2038 | Lumen Dental Group | Nov 9 | $5,997.39 | |
| INV-2037 | Marigold Events | Nov 6 | $4,916.52 | |
| INV-2036 | Atlas Physio | Nov 3 | $3,836.65 | |
| INV-2035 | Harbor & Pine Architects | Oct 30 | $2,754.78 | |
| INV-2034 | Stonebridge Schools | Oct 27 | $1,674.91 | |
| INV-2033 | Greyfield Legal LLP | Oct 23 | $593.04 | |
| INV-2032 | Kestrel Logistics | Oct 20 | $8,511.17 | |
| INV-2031 | Northwind Traders | Oct 17 | $7,431.30 | |
| INV-2030 | Orbit Analytics | Oct 13 | $6,349.43 | |
| No matching rows. | ||||
"use client"
const [selected, setSelected] = useState<string[]>([])
const total = invoices
.filter((r) => selected.includes(r.number))
.reduce((sum, r) => sum + r.amount, 0)
<DataTable
selectable
loading={loading}
columns={columns}
data={invoices}
getRowId={(r) => r.number}
onSelectionChange={setSelected}
/>Wide table
Twelve columns, far wider than the container. The checkbox and invoice columns are pinned on the left, the actions column on the right (stickyLastColumn). A fade and a round arrow button appear on the side that still has hidden columns, and each disappears at its end. Scroll with the arrows, the scrollbar, Shift + wheel, a swipe, or ← and → when the table is focused.
| Actions | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| INV-2041 | Northwind Traders ap@northwind.com | Paid | Nov 20 | Dec 20 | $240 | $48 | USD | Card | Ben Carter | Analytics | ||
| INV-2040 | Orbit Analytics ap@orbitanalytics.ai | Draft | Nov 16 | Dec 16 | $8,159 | $1,632 | USD | Bank transfer | Alice Moore | Website redesign | ||
| INV-2039 | Bramble Coffee Roasters hello@bramblecoffee.co | Overdue | Nov 13 | Dec 13 | $7,079 | $1,416 | USD | PayPal | Dan Wells | Mobile app | ||
| INV-2038 | Lumen Dental Group billing@lumendental.com | Paid | Nov 9 | Dec 9 | $5,997 | $1,199 | USD | Invoice | Chloe Reed | Brand identity | ||
| INV-2037 | Marigold Events team@marigoldevents.com | Pending | Nov 6 | Dec 6 | $4,917 | $983 | USD | Card | Ben Carter | Analytics | ||
| INV-2036 | Atlas Physio admin@atlasphysio.com | Paid | Nov 3 | Dec 3 | $3,837 | $767 | USD | Bank transfer | Alice Moore | Website redesign | ||
| INV-2035 | Harbor & Pine Architects finance@harborpine.com | Pending | Oct 30 | Nov 29 | $2,755 | $551 | USD | PayPal | Dan Wells | Mobile app | ||
| INV-2034 | Stonebridge Schools procurement@stonebridge.edu | Paid | Oct 27 | Nov 26 | $1,675 | $335 | USD | Invoice | Chloe Reed | Brand identity | ||
| INV-2033 | Greyfield Legal LLP accounts@greyfield.law | Draft | Oct 23 | Nov 22 | $593 | $119 | USD | Card | Ben Carter | Analytics | ||
| INV-2032 | Kestrel Logistics invoices@kestrel.io | Overdue | Oct 20 | Nov 19 | $8,511 | $1,702 | USD | Bank transfer | Alice Moore | Website redesign | ||
| INV-2031 | Northwind Traders ap@northwind.com | Paid | Oct 17 | Nov 16 | $7,431 | $1,486 | USD | PayPal | Dan Wells | Mobile app | ||
| INV-2030 | Orbit Analytics ap@orbitanalytics.ai | Pending | Oct 13 | Nov 12 | $6,349 | $1,270 | USD | Invoice | Chloe Reed | Brand identity | ||
| No matching rows. | ||||||||||||
// 12 columns; the last one is the "Actions" column
<DataTable
selectable
stickyLastColumn
columns={columns}
data={invoices}
getRowId={(r) => r.number}
maxHeight="20rem"
/>Empty and loading states
emptyState is shown when data is empty, noMatchState when the search finds nothing, loading renders skeleton rows.
| No disputed invoices. Nice. | ||||
<DataTable data={[]} emptyState="No disputed invoices. Nice." {...props} />
<DataTable loading loadingRows={3} {...props} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
| columns | DataTableColumn<T>[] | - | Column definitions, see below |
| data | T[] | - | Rows |
| getRowId | (row: T, index: number) => string | - | Stable id of a row; also the value of its checkbox |
| caption | ReactNode | - | Accessible name of the table |
| showCaption | boolean | false | Show the caption under the table |
| defaultSorting | { columnId: string; direction: "asc" | "desc" } | null | null | Order applied on the server |
| selectable | boolean | false | Add a checkbox column |
| defaultSelectedIds | string[] | - | Rows checked on first render |
| selectName | string | "selected" | Form field name of the row checkboxes |
| searchable | boolean | false | Search field that filters rows by their text |
| toolbar | ReactNode | - | Content next to the search field |
| pageSize | number | - | Rows per page; turns pagination on |
| pageSizeOptions | number[] | - | Show a rows per page select |
| loading | boolean | false | Render skeleton rows |
| loadingRows | number | 5 | Number of skeleton rows |
| emptyState | ReactNode | "No results." | Content when data is empty |
| noMatchState | ReactNode | "No matching rows." | Content when the search finds nothing |
| stickyFirstColumn | boolean | true | Pin the first column and the checkboxes |
| striped | boolean | false | Zebra rows |
| stickyLastColumn | boolean | false | Pin the last column (row actions) to the right edge |
| scrollButtons | boolean | true | Round arrow buttons on the edges of a table wider than its container; touch screens swipe instead |
| maxHeight | number | string | - | Height of the scroll area, so the header sticks inside |
| labels | Partial<DataTableLabels> | English | Texts for screen readers, the search and the pager |
| onSelectionChange / onSortingChange / onPageChange | function | - | Client only: selection, sorting and page callbacks |
DataTableColumn
| Field | Type | Description |
|---|---|---|
| id | string | Unique column id |
| header | ReactNode | Header content |
| accessor | (row: T) => unknown | Raw value for the default cell and for sorting |
| cell | (row: T, value: unknown) => ReactNode | Custom cell content |
| sortable | boolean | Make the header a sort button |
| sortValue | (row: T) => string | number | Date | null | Value to sort by instead of the accessor |
| align | "left" | "center" | "right" | Text alignment; numeric columns align right |
| numeric | boolean | Tabular figures |
| width | number | string | Column width |
Toolbar hooks
| Attribute | Description |
|---|---|
| data-selection-only | Shown only while at least one row is selected (CSS :has) |
| data-selected-count | Its text becomes the number of selected rows |
| data-table-clear-selection | A click clears the selection |
Features
- Server first: rows and cells render on the server; the client part only moves and hides rows
- Works without JavaScript: every row is shown, checkboxes are highlighted and submitted with a form
- Real table semantics: caption, scope, aria-sort, aria-rowindex and aria-rowcount for screen readers
- Keyboard: the scroll area is focusable, headers are buttons, checkboxes are native
- Sticky header and first column: cells keep an opaque background so nothing shows through
- Scroll arrows and edge fades: a wide table gets round arrow buttons and a fade on the sides that still hide columns; each goes away at its end, on touch screens swipe instead, and a table that fits shows none of it
- Pinned actions column: stickyLastColumn keeps the last column on the right edge with a soft shadow while columns scroll under it
- Reduced motion: no background transitions and no skeleton pulse