Data table
Updated
DataTable is the charts' data table as a control: a real table in tabular figures, its header in pixel caps over an ink rule, inside a scroll area. Sortable headers are buttons that set aria-sort, selectable adds a checkbox per row and a select-all, a max height makes the header stick, and on a phone it scrolls sideways instead of squeezing the columns. Like the charts, it takes your theme's ink and paper, light or dark, and needs no Tailwind.
Example
| Avg time | |||||
|---|---|---|---|---|---|
| / | 18,240 | +4.2% | 38% | 1:42 | |
| /blog/dither | 9,612 | +12.8% | 44% | 3:05 | |
| /charts/bar | 7,408 | -2.1% | 31% | 2:18 | |
| /docs | 6,977 | +0.6% | 27% | 4:51 | |
| /charts/line | 5,130 | +7.4% | 35% | 2:02 | |
| /changelog | 3,205 | -5.3% | 52% | 0:58 | |
| /about | 2,214 | +1.1% | 61% | 0:47 | |
| /contact | 1,086 | -0.4% | 66% | 0:39 |
1 of 8 selected
"use client" // DataTable keeps state, and cell and sortValue are functions
import { useState } from "react"
import "@/components/dithercharts/controls.css"
import { DataTable } from "@/components/dithercharts/data-table"
// The first column heads each row; a max height makes the header stick.
const [selected, setSelected] = useState<string[]>([])
<DataTable
caption="Top pages, last 7 days"
className="max-h-72"
rows={pages}
rowKey="path"
defaultSort={{ key: "visits", direction: "descending" }}
selectable
selected={selected}
onSelectedChange={setSelected}
columns={[
{ key: "path", header: "Page", sortable: true },
{ key: "visits", header: "Visits", align: "end", sortable: true, cell: (row) => row.visits.toLocaleString() },
{ key: "time", header: "Avg time", align: "end" },
]}
/>Install
Controls install with DitherCharts Pro. See Installation to log in, then import controls.css once, e.g. in your root layout.
npx dithercharts add data-table
Props
It renders a native element, so every other attribute that element takes passes through.
| Prop | Type | Default | Description |
|---|---|---|---|
| captionrequired | Type ReactNode | Default – | Names the table, shown above it in pixel caps. |
| columnsrequired | Type DataTableColumn<Row>[] | Default – | The columns, in order. The first is each row's header, as in the charts' data tables. |
| rowsrequired | Type Row[] | Default – | |
| rowKeyrequired | Type keyof Row & string | Default – | The field that tells rows apart, e.g. "id". |
| sort | Type DataTableSort | null | Default – | The column and direction the rows are sorted by, when you control it; null is the given order. |
| sort.keyrequired | Type string | Default – | |
| sort.directionrequired | Type "ascending" | "descending" | Default – | |
| defaultSort | Type DataTableSort | null | Default null | The sort to start with, when the table keeps its own. |
| defaultSort.keyrequired | Type string | Default – | |
| defaultSort.directionrequired | Type "ascending" | "descending" | Default – | |
| onSortChange | Type (sort: DataTableSort) => void | Default – | Called with the new sort when a sortable header is pressed: ascending first, then flipping. |
| selectable | Type boolean | Default false | Adds a checkbox to each row, and one in the header that selects them all. |
| selected | Type string[] | Default – | The keys of the selected rows, when you control them. |
| defaultSelected | Type string[] | Default – | The rows selected to start with, when the table keeps its own. |
| onSelectedChange | Type (selected: string[]) => void | Default – | Called with the keys of the selected rows whenever they change. |

