Organisms · Data table
Data table
Rows of records that people read, compare and sort. It's a real HTML table with a caption, column and row headers, and sortable columns. When there's a job to do, each row gets a checkbox.
- Best for
- Records people read, compare and sort across columns, with optional bulk selection.
- Not for
- A list people scan row by row: use ListItem. A form in every cell.
This page as Markdown, a plain-text copy for a coding agent (an AI tool that writes code).
Examples
Sortable
Sorted by Reference, ascending
| Destination | Status | |||
|---|---|---|---|---|
| SHP-201 | Cluj depot | 8.2 | 5 | In transit |
| SHP-202 | Arad depot | 14.0 | 12 | Delivered |
| SHP-203 | Brasov depot | 3.5 | 2 | Held |
| SHP-204 | Sibiu depot | 22.1 | 9 | In transit |
| SHP-205 | Oradea depot | 6.0 | 1 | Delivered |
| SHP-206 | Timisoara depot | 18.4 | 15 | Held |
| SHP-207 | Iasi depot | 9.9 | 4 | In transit |
| SHP-208 | Constanta depot | 27.3 | 20 | Delivered |
Code
tsx
type Status = 'In transit' | 'Delivered' | 'Held'type Shipment = { ref: string; destination: string; weight: number; items: number; status: Status }const SHIPMENTS: Shipment[] = [ { ref: 'SHP-201', destination: 'Cluj depot', weight: 8.2, items: 5, status: 'In transit' }, { ref: 'SHP-202', destination: 'Arad depot', weight: 14.0, items: 12, status: 'Delivered' }, { ref: 'SHP-203', destination: 'Brasov depot', weight: 3.5, items: 2, status: 'Held' }, { ref: 'SHP-204', destination: 'Sibiu depot', weight: 22.1, items: 9, status: 'In transit' }, { ref: 'SHP-205', destination: 'Oradea depot', weight: 6.0, items: 1, status: 'Delivered' }, { ref: 'SHP-206', destination: 'Timisoara depot', weight: 18.4, items: 15, status: 'Held' }, { ref: 'SHP-207', destination: 'Iasi depot', weight: 9.9, items: 4, status: 'In transit' }, { ref: 'SHP-208', destination: 'Constanta depot', weight: 27.3, items: 20, status: 'Delivered' },]const STATUS_TONE: Record<Status, 'info' | 'ok' | 'warning'> = { 'In transit': 'info', Delivered: 'ok', Held: 'warning',}const COLUMNS: DataTableColumn<Shipment>[] = [ { key: 'ref', header: 'Reference', cell: s => s.ref, sortValue: s => s.ref }, { key: 'destination', header: 'Destination', cell: s => s.destination }, { key: 'weight', header: 'Weight (kg)', cell: s => s.weight.toFixed(1), sortValue: s => s.weight, numeric: true }, { key: 'items', header: 'Items', cell: s => String(s.items), sortValue: s => s.items, numeric: true }, { key: 'status', header: 'Status', cell: s => ( <Badge emphasis="solid" tone={STATUS_TONE[s.status]}> {s.status} </Badge> ), },]<DataTable caption="Shipments, sortable" columns={COLUMNS} rows={SHIPMENTS} getRowId={s => s.ref} rowHeader="ref" defaultSort={{ key: 'ref', direction: 'ascending' }}/>Controlled sort: the page, not the table, keeps track of the sort order
Sorted by Weight (kg), descending
| Destination | Status | |||
|---|---|---|---|---|
| SHP-208 | Constanta depot | 27.3 | 20 | Delivered |
| SHP-204 | Sibiu depot | 22.1 | 9 | In transit |
| SHP-206 | Timisoara depot | 18.4 | 15 | Held |
| SHP-202 | Arad depot | 14.0 | 12 | Delivered |
| SHP-207 | Iasi depot | 9.9 | 4 | In transit |
| SHP-201 | Cluj depot | 8.2 | 5 | In transit |
| SHP-205 | Oradea depot | 6.0 | 1 | Delivered |
| SHP-203 | Brasov depot | 3.5 | 2 | Held |
Code
tsx
type Status = 'In transit' | 'Delivered' | 'Held'type Shipment = { ref: string; destination: string; weight: number; items: number; status: Status }const SHIPMENTS: Shipment[] = [ { ref: 'SHP-201', destination: 'Cluj depot', weight: 8.2, items: 5, status: 'In transit' }, { ref: 'SHP-202', destination: 'Arad depot', weight: 14.0, items: 12, status: 'Delivered' }, { ref: 'SHP-203', destination: 'Brasov depot', weight: 3.5, items: 2, status: 'Held' }, { ref: 'SHP-204', destination: 'Sibiu depot', weight: 22.1, items: 9, status: 'In transit' }, { ref: 'SHP-205', destination: 'Oradea depot', weight: 6.0, items: 1, status: 'Delivered' }, { ref: 'SHP-206', destination: 'Timisoara depot', weight: 18.4, items: 15, status: 'Held' }, { ref: 'SHP-207', destination: 'Iasi depot', weight: 9.9, items: 4, status: 'In transit' }, { ref: 'SHP-208', destination: 'Constanta depot', weight: 27.3, items: 20, status: 'Delivered' },]const STATUS_TONE: Record<Status, 'info' | 'ok' | 'warning'> = { 'In transit': 'info', Delivered: 'ok', Held: 'warning',}const COLUMNS: DataTableColumn<Shipment>[] = [ { key: 'ref', header: 'Reference', cell: s => s.ref, sortValue: s => s.ref }, { key: 'destination', header: 'Destination', cell: s => s.destination }, { key: 'weight', header: 'Weight (kg)', cell: s => s.weight.toFixed(1), sortValue: s => s.weight, numeric: true }, { key: 'items', header: 'Items', cell: s => String(s.items), sortValue: s => s.items, numeric: true }, { key: 'status', header: 'Status', cell: s => ( <Badge emphasis="solid" tone={STATUS_TONE[s.status]}> {s.status} </Badge> ), },]function ControlledSortExample() { const [sort, setSort] = useState<DataTableSort>({ key: 'weight', direction: 'descending' }) return ( <DataTable caption="Shipments, controlled sort" columns={COLUMNS} rows={SHIPMENTS} getRowId={s => s.ref} rowHeader="ref" sort={sort} onSortChange={setSort} /> )}<ControlledSortExample />With row selection and an action that applies to every selected row
| Destination | Status | ||||
|---|---|---|---|---|---|
| SHP-201 | Cluj depot | 8.2 | 5 | In transit | |
| SHP-202 | Arad depot | 14.0 | 12 | Delivered | |
| SHP-203 | Brasov depot | 3.5 | 2 | Held | |
| SHP-204 | Sibiu depot | 22.1 | 9 | In transit | |
| SHP-205 | Oradea depot | 6.0 | 1 | Delivered | |
| SHP-206 | Timisoara depot | 18.4 | 15 | Held | |
| SHP-207 | Iasi depot | 9.9 | 4 | In transit | |
| SHP-208 | Constanta depot | 27.3 | 20 | Delivered |
Code
tsx
type Status = 'In transit' | 'Delivered' | 'Held'type Shipment = { ref: string; destination: string; weight: number; items: number; status: Status }const SHIPMENTS: Shipment[] = [ { ref: 'SHP-201', destination: 'Cluj depot', weight: 8.2, items: 5, status: 'In transit' }, { ref: 'SHP-202', destination: 'Arad depot', weight: 14.0, items: 12, status: 'Delivered' }, { ref: 'SHP-203', destination: 'Brasov depot', weight: 3.5, items: 2, status: 'Held' }, { ref: 'SHP-204', destination: 'Sibiu depot', weight: 22.1, items: 9, status: 'In transit' }, { ref: 'SHP-205', destination: 'Oradea depot', weight: 6.0, items: 1, status: 'Delivered' }, { ref: 'SHP-206', destination: 'Timisoara depot', weight: 18.4, items: 15, status: 'Held' }, { ref: 'SHP-207', destination: 'Iasi depot', weight: 9.9, items: 4, status: 'In transit' }, { ref: 'SHP-208', destination: 'Constanta depot', weight: 27.3, items: 20, status: 'Delivered' },]const STATUS_TONE: Record<Status, 'info' | 'ok' | 'warning'> = { 'In transit': 'info', Delivered: 'ok', Held: 'warning',}const COLUMNS: DataTableColumn<Shipment>[] = [ { key: 'ref', header: 'Reference', cell: s => s.ref, sortValue: s => s.ref }, { key: 'destination', header: 'Destination', cell: s => s.destination }, { key: 'weight', header: 'Weight (kg)', cell: s => s.weight.toFixed(1), sortValue: s => s.weight, numeric: true }, { key: 'items', header: 'Items', cell: s => String(s.items), sortValue: s => s.items, numeric: true }, { key: 'status', header: 'Status', cell: s => ( <Badge emphasis="solid" tone={STATUS_TONE[s.status]}> {s.status} </Badge> ), },]function BulkExample() { const [rows, setRows] = useState(SHIPMENTS) const [selected, setSelected] = useState<Set<string>>(new Set()) const count = selected.size const archive = () => { if (count === 0) return setRows(current => current.filter(row => !selected.has(row.ref))) setSelected(new Set()) } return ( <div className="flex flex-col gap-4 w-full"> <Button variant="secondary" aria-disabled={count === 0 || undefined} onClick={archive} className="self-start"> {count > 0 ? `Archive ${count} shipment${count === 1 ? '' : 's'}` : 'Archive shipments'} </Button> <DataTable caption="Shipments, selectable" columns={COLUMNS} rows={rows} getRowId={s => s.ref} rowHeader="ref" selectable rowLabel={s => s.ref} selected={selected} onSelectedChange={setSelected} /> </div> )}<BulkExample />With pagination and selection
| Destination | Status | ||||
|---|---|---|---|---|---|
| SHP-201 | Cluj depot | 8.2 | 5 | In transit | |
| SHP-202 | Arad depot | 14.0 | 12 | Delivered | |
| SHP-203 | Brasov depot | 3.5 | 2 | Held | |
| SHP-204 | Sibiu depot | 22.1 | 9 | In transit |
Code
tsx
type Status = 'In transit' | 'Delivered' | 'Held'type Shipment = { ref: string; destination: string; weight: number; items: number; status: Status }const SHIPMENTS: Shipment[] = [ { ref: 'SHP-201', destination: 'Cluj depot', weight: 8.2, items: 5, status: 'In transit' }, { ref: 'SHP-202', destination: 'Arad depot', weight: 14.0, items: 12, status: 'Delivered' }, { ref: 'SHP-203', destination: 'Brasov depot', weight: 3.5, items: 2, status: 'Held' }, { ref: 'SHP-204', destination: 'Sibiu depot', weight: 22.1, items: 9, status: 'In transit' }, { ref: 'SHP-205', destination: 'Oradea depot', weight: 6.0, items: 1, status: 'Delivered' }, { ref: 'SHP-206', destination: 'Timisoara depot', weight: 18.4, items: 15, status: 'Held' }, { ref: 'SHP-207', destination: 'Iasi depot', weight: 9.9, items: 4, status: 'In transit' }, { ref: 'SHP-208', destination: 'Constanta depot', weight: 27.3, items: 20, status: 'Delivered' },]const STATUS_TONE: Record<Status, 'info' | 'ok' | 'warning'> = { 'In transit': 'info', Delivered: 'ok', Held: 'warning',}const COLUMNS: DataTableColumn<Shipment>[] = [ { key: 'ref', header: 'Reference', cell: s => s.ref, sortValue: s => s.ref }, { key: 'destination', header: 'Destination', cell: s => s.destination }, { key: 'weight', header: 'Weight (kg)', cell: s => s.weight.toFixed(1), sortValue: s => s.weight, numeric: true }, { key: 'items', header: 'Items', cell: s => String(s.items), sortValue: s => s.items, numeric: true }, { key: 'status', header: 'Status', cell: s => ( <Badge emphasis="solid" tone={STATUS_TONE[s.status]}> {s.status} </Badge> ), },]function PagedExample() { const [perPage, setPerPage] = useState(4) const [page, setPage] = useState(1) const [selected, setSelected] = useState<Set<string>>(new Set()) const start = (page - 1) * perPage const visible = SHIPMENTS.slice(start, start + perPage) return ( <div className="flex flex-col gap-3 w-full"> <DataTable caption="Shipments, paged" columns={COLUMNS} rows={visible} getRowId={s => s.ref} rowHeader="ref" selectable rowLabel={s => s.ref} selected={selected} onSelectedChange={setSelected} /> <Pagination page={page} pageCount={Math.ceil(SHIPMENTS.length / perPage)} onPageChange={setPage} totalItems={SHIPMENTS.length} pageSize={perPage} onPageSizeChange={size => { setPerPage(size) setPage(1) }} pageSizeOptions={[4, 8, 12]} label="Pagination, shipments" /> </div> )}<PagedExample />Empty
| Destination | Status | |||
|---|---|---|---|---|
No returns yet. | ||||
Code
tsx
type Status = 'In transit' | 'Delivered' | 'Held'type Shipment = { ref: string; destination: string; weight: number; items: number; status: Status }const STATUS_TONE: Record<Status, 'info' | 'ok' | 'warning'> = { 'In transit': 'info', Delivered: 'ok', Held: 'warning',}const COLUMNS: DataTableColumn<Shipment>[] = [ { key: 'ref', header: 'Reference', cell: s => s.ref, sortValue: s => s.ref }, { key: 'destination', header: 'Destination', cell: s => s.destination }, { key: 'weight', header: 'Weight (kg)', cell: s => s.weight.toFixed(1), sortValue: s => s.weight, numeric: true }, { key: 'items', header: 'Items', cell: s => String(s.items), sortValue: s => s.items, numeric: true }, { key: 'status', header: 'Status', cell: s => ( <Badge emphasis="solid" tone={STATUS_TONE[s.status]}> {s.status} </Badge> ), },]<DataTable caption="Returns" columns={COLUMNS} rows={[]} getRowId={s => s.ref} rowHeader="ref" emptyMessage="No returns yet." />Anatomy
| Part | What it is |
|---|---|
| Caption | Names the table and its scroll region. You can hide it visually and it still works for screen readers. |
| Scroll region | It can take keyboard focus, so a wide table scrolls from the keyboard. |
| Column header | A sort button when the column sorts. The sorted column carries aria-sort, an ARIA (Accessible Rich Internet Applications) attribute that tells screen readers which way it's sorted. |
| Row header | The cell from the rowHeader column. It names its row. |
| Selection | A checkbox per row, one to select all, and a count above the table. Selected rows don't get a tint. |
| Empty | One cell that spans every column. |
Props
- captionstring
- Required. Names the table and its scroll region.
- hideCaptionbooleanDefault false
- Hides the caption visually. It still names the table and its scroll region for screen readers.
- columnsDataTableColumn<Row>[]
- Required. Each column has key, header, cell, sortValue and numeric.
- rowsreadonly Row[]
- Required.
- getRowId(row: Row) => string
- Required.
- rowHeaderstring
- Required. The key of the column that heads each row.
- defaultSortDataTableSort
- Starts the table sorted. Uncontrolled: the table keeps track of the sort itself after that.
- sortDataTableSort
- Controlled sort, meaning your code holds the sort and passes it in. Set it together with onSortChange. A header click then only reports the next sort.
- onSortChange(sort: DataTableSort) => void
- Called with the next sort on every header click, whether sort is controlled or not.
- emptyMessageReactNodeDefault 'Nothing to show.'
- Shown across every column when rows is empty.
- selectablebooleanDefault false
- Turns on the checkbox column.
- rowLabel(row: Row) => string
- Required when selectable. Gives each row's checkbox its name.
- selectedReadonlySet<string>
- Controlled selection (your code holds it), keyed by getRowId.
- defaultSelectedIterable<string>
- The starting selection when it's uncontrolled (the table holds it).
- onSelectedChange(selected: Set<string>) => void
- Called with the next selection on every change.
- classNamestring
- Goes on the table element.
- refRef<HTMLTableElement>
- Points at the table element.
Keyboard
| Keys | Result |
|---|---|
| Tab | Moves to the scroll region, then to each sort button and checkbox in order. |
| Enter | Sorts by that column, or flips its direction. |
| Space | Sorts, or ticks a checkbox. |
| ←→ | Scroll the region sideways while it has focus. |
Do and don't
Do
- Right-align numbers and mark their column as numeric.
- Say how many rows there are near the table, and put Pagination below it.
- Keep a bulk action's button a tab stop (a place the Tab key lands) while nothing is selected, and mark it aria-disabled so screen readers say it's unavailable.
- You can page or filter the rows freely. Select-all acts on the rows shown and keeps what was selected elsewhere, and the count says how many selected rows are out of view.
Don't
- Don't make every column sortable. Sort the ones people compare.
- Don't tint selected or hovered rows. The checkbox and the count already say it.
- Don't put a form in every cell. That's a different component.