Skip to content

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

Shipments, sortable
DestinationStatus
SHP-201Cluj depot8.25In transit
SHP-202Arad depot14.012Delivered
SHP-203Brasov depot3.52Held
SHP-204Sibiu depot22.19In transit
SHP-205Oradea depot6.01Delivered
SHP-206Timisoara depot18.415Held
SHP-207Iasi depot9.94In transit
SHP-208Constanta depot27.320Delivered
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

Shipments, controlled sort
DestinationStatus
SHP-208Constanta depot27.320Delivered
SHP-204Sibiu depot22.19In transit
SHP-206Timisoara depot18.415Held
SHP-202Arad depot14.012Delivered
SHP-207Iasi depot9.94In transit
SHP-201Cluj depot8.25In transit
SHP-205Oradea depot6.01Delivered
SHP-203Brasov depot3.52Held
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

Shipments, selectable
DestinationStatus
SHP-201Cluj depot8.25In transit
SHP-202Arad depot14.012Delivered
SHP-203Brasov depot3.52Held
SHP-204Sibiu depot22.19In transit
SHP-205Oradea depot6.01Delivered
SHP-206Timisoara depot18.415Held
SHP-207Iasi depot9.94In transit
SHP-208Constanta depot27.320Delivered
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

Shipments, paged
DestinationStatus
SHP-201Cluj depot8.25In transit
SHP-202Arad depot14.012Delivered
SHP-203Brasov depot3.52Held
SHP-204Sibiu depot22.19In 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

Returns
DestinationStatus
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

PartWhat it is
CaptionNames the table and its scroll region. You can hide it visually and it still works for screen readers.
Scroll regionIt can take keyboard focus, so a wide table scrolls from the keyboard.
Column headerA 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 headerThe cell from the rowHeader column. It names its row.
SelectionA checkbox per row, one to select all, and a count above the table. Selected rows don't get a tint.
EmptyOne 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

KeysResult
TabMoves to the scroll region, then to each sort button and checkbox in order.
EnterSorts by that column, or flips its direction.
SpaceSorts, 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.