# Table (@limerdev/zest) A table of data: `` (in a container that scrolls sideways when it is too wide) holds a `Table.Header`, a `Table.Body` and an optional `Table.Footer`, each of `Table.Row`s of `Table.Head` (column headings) and `Table.Cell`s, and a `Table.Caption` that names it. Rows highlight on hover; `data-state="selected"` on a row marks it selected. A `Table.Head` with `sort` (`ascending`, `descending` or `none`) is a sort button with `aria-sort` that calls `onSort`. It is the native table, so screen readers read it as one; the app sorts, selects and pages the rows. - Category: Data display - Import: `import { Table } from '@limerdev/zest'` - Exports: `Table.Root`, `Table.Header`, `Table.Body`, `Table.Footer`, `Table.Row`, `Table.Head`, `Table.Cell`, `Table.Caption` - Use for: Records people compare across columns: invoices, users, time entries. - Don't: Laying out a page or a form: use CSS grid. A list of one-line items: a list. ```svelte Invoices this month Invoice Status Amount 1042 Paid 4 200 kr ``` Props of `Table.Head`: - `onSort` (`(() => void)`, default `undefined`): Called when the sort button is clicked; the app sets the next `sort`. - `sort` (`"none" | "ascending" | "descending"`, default `undefined`): Makes the column sortable: the heading becomes a button with an arrow, and the cell gets `aria-sort`. `none` is sortable but not sorted now. Left unset, the column isn't sortable. The app sorts the rows. Example: Basic A caption names the table; Table.Footer holds the totals. ```svelte Invoices this month Invoice Customer Status Amount {#each invoices as invoice (invoice.number)} {invoice.number} {invoice.customer} {invoice.status} {kr.format(invoice.amount)} {/each} Total {kr.format(total)} ``` Example: Selection Rows with a Checkbox: data-state="selected" marks a chosen row. The heading's Checkbox selects all, and is indeterminate while only some are. ```svelte {selected.length} of {users.length} users selected 0 && selected.length < users.length} onCheckedChange={toggleAll} /> Name Email {#each users as user (user.id)} toggle(user.id, checked)} /> {user.name} {user.email} {/each} ``` Example: Sorting sort on Table.Head makes the heading a button with aria-sort; onSort is yours, and so is the sorting. ```svelte Invoices, sorted by {column} Invoice sortBy('client')}> Client sortBy('amount')} > Amount {#each rows as invoice (invoice.id)} {invoice.id} {invoice.client} {invoice.amount} kr {/each} ```