DitherCharts

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

Top pages, last 7 days
Avg time
/18,240+4.2%38%1:42
/blog/dither9,612+12.8%44%3:05
/charts/bar7,408-2.1%31%2:18
/docs6,977+0.6%27%4:51
/charts/line5,130+7.4%35%2:02
/changelog3,205-5.3%52%0:58
/about2,214+1.1%61%0:47
/contact1,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.

DataTable props
PropTypeDefaultDescription
captionrequiredType ReactNodeDefault –Names the table, shown above it in pixel caps.
columnsrequiredType DataTableColumn<Row>[]Default –The columns, in order. The first is each row's header, as in the charts' data tables.
rowsrequiredType Row[]Default –
rowKeyrequiredType keyof Row & stringDefault –The field that tells rows apart, e.g. "id".
sortType DataTableSort | nullDefault –The column and direction the rows are sorted by, when you control it; null is the given order.
sort.keyrequiredType stringDefault –
sort.directionrequiredType "ascending" | "descending"Default –
defaultSortType DataTableSort | nullDefault nullThe sort to start with, when the table keeps its own.
defaultSort.keyrequiredType stringDefault –
defaultSort.directionrequiredType "ascending" | "descending"Default –
onSortChangeType (sort: DataTableSort) => voidDefault –Called with the new sort when a sortable header is pressed: ascending first, then flipping.
selectableType booleanDefault falseAdds a checkbox to each row, and one in the header that selects them all.
selectedType string[]Default –The keys of the selected rows, when you control them.
defaultSelectedType string[]Default –The rows selected to start with, when the table keeps its own.
onSelectedChangeType (selected: string[]) => voidDefault –Called with the keys of the selected rows whenever they change.