# Table view

A read-only table from columns and rows: right-aligned tabular figures, a footer row, a custom cell snippet and a sideways scroll below its minimum width.

Section: Components / Data display
Page: https://ui.heyramzi.com/components/table-view
Import path: `@heyramzi/ui/components/table-view`

## Import

```ts
import { TableView, type TableColumn } from "@heyramzi/ui/components/table-view";
import { SectionCard } from "@heyramzi/ui/components/section-card";
import * as ContextMenu from "@heyramzi/ui/shadcn/context-menu";
```

## Usage

```svelte
<TableView
  columns={[
    { key: "item", label: "Item" },
    { key: "hours", label: "Hours", numeric: true, width: "6rem" },
  ]}
  rows={lines}
  rowKey={(row) => row.id}
  label="Quote lines"
/>
```

## Demo

```svelte
<script lang="ts">
	import { TableView, type TableColumn } from "@heyramzi/ui/components/table-view";
	import { SectionCard } from "@heyramzi/ui/components/section-card";
	import * as ContextMenu from "@heyramzi/ui/shadcn/context-menu";

	type Line = { id: string; item: string; hours: number; total: number };

	let lines = $state<Line[]>([
		{ id: "audit", item: "Audit and scoping", hours: 12, total: 1800 },
		{ id: "build", item: "Build the first module", hours: 40, total: 6000 },
		{ id: "handover", item: "Handover and training", hours: 8, total: 1200 },
	]);
	const columns: TableColumn<Line>[] = [
		{ key: "item", label: "Item" },
		{ key: "hours", label: "Hours", numeric: true, width: "6rem" },
		{
			key: "total",
			label: "Total",
			numeric: true,
			width: "8rem",
			format: (v) => `$${Number(v).toLocaleString("en-US")}`,
		},
	];
	let footer = $derived<Line>({
		id: "sum",
		item: "Total",
		hours: lines.reduce((sum, line) => sum + line.hours, 0),
		total: lines.reduce((sum, line) => sum + line.total, 0),
	});
</script>

<SectionCard title="Quote lines" flush class="w-full max-w-2xl">
	<TableView {columns} rows={lines} rowKey={(row) => row.id} label="Quote lines" {footer}>
		{#snippet rowMenu(row)}
			<ContextMenu.Item onclick={() => (lines = lines.filter((line) => line.id !== row.id))}>
				Remove line
			</ContextMenu.Item>
		{/snippet}
	</TableView>
</SectionCard>
```

## API

| Prop | Type | Default | Notes |
| --- | --- | --- | --- |
| `columns` | `TableColumn<Row>[]` |  | required |
| `rows` | `Row[]` |  | required |
| `rowKey` | `(row: Row) => string` |  | required. What names a row, so a filter keeps its rows in place. |
| `label` | `string` |  | required. What the table is, for a screen reader. |
| `footer` | `Row` |  | A totals row, drawn under the body in the same columns. |
| `cell` | `Snippet<[TableCell<Row>]>` |  | Draws a cell its own way. Print `text` for the plain case. |
| `rowClass` | `(row: Row) => string \| undefined` |  |  |
| `minWidth` | `string` |  | Below this width the table scrolls sideways inside its frame. Defaults to the columns' own widths (8rem a text column, 5rem a number), so a phone scrolls rather than splits words. Pass "0" for a table that should squeeze. |
| `class` | `string` |  |  |
| `sort` | `SortState<Row>` | `null` | bindable. The sorted column and its direction. Bind it; columns with `sortable` set it. |
| `rowHref` | `(row: Row) => string \| undefined` |  | Where a row goes. Draws a real link over the row, so middle-click and cmd-click work. |
| `onRowLink` | `(row: Row, event: MouseEvent) => void` |  | Runs on a plain left click of the row link. Call `event.preventDefault()` to open in place; a modified click still follows `href`. |
| `rowLabel` | `(row: Row) => string` |  | The link's accessible name. Defaults to the row's first column text. |
| `selectable` | `boolean` | `false` | Adds a checkbox column with a select-all in the head. |
| `selected` | `Set<string>` | `new Set<string>()` | bindable. The `rowKey` of every checked row. Bind it. |
| `selectAllLabel` | `string` | `"Select all rows"` |  |
| `selectRowLabel` | `(row: Row) => string` |  |  |
| `openColumnLabel` | `string` | `"Open"` | The hidden head of the last column, the one holding each row's link or button. |
| `rowMenu` | `Snippet<[Row]>` |  | The items of a row menu: `ContextMenu.Item`s. Right-click, a long press, Shift+F10 or the ContextMenu key opens it. |
| `group` | `(row: Row) => string \| null` |  | Rows that return the same key sit under one header row, and `null` rows sit with no header. Order `rows` so a group is contiguous. |
| `groupHeader` | `Snippet<[TableGroup<Row>]>` |  | Draws a group's header. Prints the key when left out. |
| `collapsible` | `boolean` | `false` | Makes each group header a button that folds its rows. |
| `collapsed` | `Set<string>` | `new Set<string>()` | bindable. The keys of the folded groups. Bind it. |
| `head` | `Snippet<[TableColumn<Row>]>` |  | Draws a column head its own way, an icon or a colour. Print `column.label` for the plain case. |
| `onRowClick` | `(row: Row) => void` |  | The row opens in place, from a button over the row named like the row link. |
| `expanded` | `(row: Row) => boolean` |  | Whether a row shows its `subRow`. |
| `subRow` | `Snippet<[Row]>` |  | A full-width row under the row, for detail. |
| `empty` | `Snippet` |  | Drawn across every column when there are no rows. |
| `trailing` | `Snippet` |  | Drawn across every column under the last row: a load-more sentinel or a spinner. |
| `loading` | `boolean` | `false` | Draws placeholder rows in the same frame instead of `rows`. |
| `skeletonRows` | `number` | `5` |  |
