Display
DataTable
Table with search, sorting, pagination, column visibility, row groups, resizable columns, drag-to-reorder rows and interactive cells, built on TanStack Table.
DataTable lives in its own entry point so that TanStack Table stays out of bundles that do not need it. Install the optional peer first.
bun add @tanstack/svelte-tablenpm i @tanstack/svelte-tablepnpm add @tanstack/svelte-table<script lang="ts">
import { DataTable, createCaColumnHelper } from "@codeatlasdev/ui/table";
type User = { name: string };
const users: User[] = [{ name: "Ana Souza" }, { name: "Bruno Lima" }];
const col = createCaColumnHelper<User>();
const columns = col.columns([
col.accessor("name", { header: "Name", meta: { label: "Name" } }),
]);
</script>
<DataTable {columns} data={users} />
There are two modes. In simple mode you pass columns and data and the table searches, sorts and paginates locally. In controlled mode you pass globalFilter, pagination and sorting together with their change handlers, and feed it one page of data plus total, which is what you want for server-side data.
Simple mode
Pass `columns` and `data`. Search, sort and pagination are managed automatically.
| ID | Name | Role | Status | |
|---|---|---|---|---|
| 1 | Ana Souza | ana@example.com | Admin | active |
| 2 | Bruno Lima | bruno@example.com | Developer | active |
| 3 | Carla Mendes | carla@example.com | Designer | inactive |
| 4 | Diego Ferreira | diego@example.com | Developer | active |
| 5 | Elena Castro | elena@example.com | Manager | pending |
| 6 | Felipe Rocha | felipe@example.com | Developer | active |
| 7 | Gabriela Nunes | gabi@example.com | Designer | active |
| 8 | Henrique Lopes | henrique@example.com | Admin | inactive |
| 9 | Isabela Torres | isa@example.com | Developer | active |
| 10 | Joao Almeida | joao@example.com | Manager | pending |
Reference #123 · Pick in the gallery
Code
Controlled mode
Own the state to drive server-side filtering, sorting and pagination.
| ID | Name | Role | Status | |
|---|---|---|---|---|
| 1 | Ana Souza | ana@example.com | Admin | active |
| 2 | Bruno Lima | bruno@example.com | Developer | active |
| 3 | Carla Mendes | carla@example.com | Designer | inactive |
| 4 | Diego Ferreira | diego@example.com | Developer | active |
| 5 | Elena Castro | elena@example.com | Manager | pending |
Reference #122 · Pick in the gallery
Code
Groups, resize, reorder and interactive cells
Four options turn the table into a backlog or board in table form. They are independent: use any of them alone.
- Row groups.
groupBy={(row) => row.sprint}renders one<tbody>per key, in first-appearance order, each with a header row. The header is a button witharia-expandedthat collapses its rows;groupLabelreplaces its content. - Column resize. Set
meta: { resizable: true }on a column, withsize,minSizeandmaxSizeon the definition. The header gets a handle that follows the window splitter pattern: drag it with a mouse, pen or finger, or focus it and press the arrow keys (10px, 40px with Shift), Home (minimum), End (maximum) and Enter (reset). Double click resets too. Once a column is resizable the table uses a fixed layout and scrolls sideways when its columns are wider than the container. - Row reorder.
onReorderadds a drag grip to every row, built on the drag and drop primitive: mouse, touch (press and hold) and keyboard (Space lifts, arrows move, Enter drops, Escape cancels), announced to screen readers. Groups exchange rows.DataTablenever changesdata: the handler gets{ id, from, to }with{ group, index }positions inside a group and you apply them. Give rows stable ids withrowId. - Interactive cells. A cell can render any component (
renderComponent(Button, …), menus, inline editors). A click on a link, button, input or other control inside a cell does not reachonRowClick; mark any other element withdata-no-row-clickto opt it out too.
Only the current page is in the DOM, so a table of thousands of rows stays fast: raise pageSize (simple mode) or page on the server (controlled mode).
Groups
Collapsible sprints, a resizable Task column, rows that move between sprints by drag or keyboard, and a Button inside a cell.
Press Space to lift, the arrow keys to move, Enter to drop and Escape to cancel. Touch: press and hold, then drag.
| Reorder | Owner | Details | |
|---|---|---|---|
| Audit contrast tokens | Ana | ||
| Write the export guide | Bruno | ||
| Ship the date picker fix | Carla | ||
| Wire the plugin install | Diego | ||
| Review the rail magnification | Elena | ||
Nothing opened yet
Reference #433 · Pick in the gallery
Code
Props
| Prop | Type | Default | Description |
|---|---|---|---|
columns | ColumnDef[] | – | Column definitions created with `createCaColumnHelper`. Required. |
data | TData[] | – | Row data. In simple mode pass all rows; in controlled mode pass the current page. Required. |
total | number | data.length | Total row count after filtering, used to compute the page count. Ignored in simple mode. |
globalFilter | string | undefined | Search value. Passing it switches the table to controlled mode. |
onGlobalFilterChange | (v: string) => void | undefined | Called when the user types in the search box. |
pagination | PaginationState | undefined | Controlled `{ pageIndex, pageSize }`. |
onPaginationChange | (v: Updater<PaginationState>) => void | undefined | Called when the page or page size changes. |
sorting | SortingState | undefined | Controlled sorting, `[{ id, desc }]`. |
onSortingChange | (v: Updater<SortingState>) => void | undefined | Called when the user clicks a column header. |
labels | { noResults?, noResultsSub?, clearSearch? } | undefined | Overrides the strings of the no-results state. |
searchPlaceholder | string | undefined | Placeholder of the search input. |
isLoading | boolean | undefined | Shows a loading indicator in the toolbar. |
isExporting | boolean | undefined | Shows the export button as busy. |
onExport | () => void | undefined | Enables the export button in the toolbar. |
onRowClick | (row: TData) => void | undefined | Called when a row is clicked. Clicks on a control inside a cell (link, button, input, `data-no-row-click`) are ignored. |
rowId | (row: TData) => string | undefined | Stable id of a row. Defaults to its index; set it when you use `onReorder`. |
groupBy | (row: TData) => string | undefined | Rows with the same key share a collapsible `<tbody>`, in first-appearance order. |
groupLabel | Snippet<[{ group: string; count: number }]> | undefined | Custom content of a group header button. |
onReorder | (e: DataTableReorderEvent) => void | undefined | Shows a drag grip on every row and reports `{ id, from, to }` with `{ group, index }` positions. You apply the move to `data`. |
pageSize | number | 10 | Rows per page in simple mode. |
hasActiveFilters | boolean | undefined | Treats the table as filtered, which suppresses the empty state. |
emptyState | Snippet | undefined | Custom content shown when there is no data and no active filter. |
children | Snippet | undefined | Filter chips such as `DataTableFacetedFilter`, rendered in the toolbar. |
class | string | '' | Extra class names on the wrapper. |
Building blocks
DataTable is assembled from the parts below, all exported from @codeatlasdev/ui/table. You rarely need them: use them standalone when you render your own <table> markup around a table instance made with createCaTable, for example a layout DataTable does not offer. Inside DataTable the toolbar, pagination and column toggle are already wired. Use DataTableColumnHeader as a column header to get a sortable header, and pass DataTableFacetedFilter as children of DataTable.
<script lang="ts">
import { renderComponent } from "@tanstack/svelte-table";
import { createCaColumnHelper, DataTableColumnHeader } from "@codeatlasdev/ui/table";
type User = { name: string };
const col = createCaColumnHelper<User>();
const columns = col.columns([
col.accessor("name", {
header: ({ column }) =>
renderComponent(DataTableColumnHeader<User, string>, { column, title: "Name" }),
meta: { label: "Name" },
}),
]);
</script>
createCaColumnHelper, createCaTable, caTableFeatures and the CaColumn, CaColumnDef, CaColumnMeta, CaTable and CaTableFeatures types are exported from the same entry point. meta.label is the name shown in the column visibility menu.
DataTableToolbar props
| Prop | Type | Default | Description |
|---|---|---|---|
table | CaTable<TData> | – | Table instance from `createCaTable`. Required. |
total | number | – | Result count shown in the toolbar. Required. |
globalFilter | string | – | Current search value. Required. |
onGlobalFilterChange | (v: string) => void | – | Called when the user types in the search box. Required. |
searchPlaceholder | string | undefined | Placeholder and accessible name of the search input. |
isLoading | boolean | undefined | Shows a spinner. |
isExporting | boolean | undefined | Disables the export button and shows a spinner in it. |
onExport | () => void | undefined | Renders the export button. It is disabled while `total` is 0. |
children | Snippet | undefined | Filter controls rendered next to the search box. |
The toolbar always includes DataTableColumnToggle on its right side.
DataTablePagination props
| Prop | Type | Default | Description |
|---|---|---|---|
table | CaTable<TData> | – | Table instance from `createCaTable`. Required. |
total | number | – | Total row count shown next to the page indicator. Required. |
It renders a page size select (10, 20, 50 or 100 rows), a page / pages indicator and previous and next buttons.
DataTableColumnHeader props
| Prop | Type | Default | Description |
|---|---|---|---|
column | CaColumn<TData, TValue> | – | Column the header belongs to. Required. |
title | string | – | Header text. Required. |
class | string | '' | Extra class names. |
A sortable column renders a button that toggles sorting and shows the direction icon, and its <th> carries aria-sort. A column that cannot sort renders the plain title.
DataTableColumnResize props
| Prop | Type | Default | Description |
|---|---|---|---|
column | CaColumn<TData, TValue> | – | Column to resize. Required. |
label | string | – | Accessible name of the handle, such as `Resize Task`. Required. |
step | number | 10 | Keyboard step in px; Shift uses four times as much. |
DataTable renders it for every column with meta.resizable. Use it in your own header markup when you build the table yourself.
DataTableColumnToggle props
| Prop | Type | Default | Description |
|---|---|---|---|
table | CaTable<TData> | – | Table instance from `createCaTable`. Required. |
A menu of checkboxes, one per column that can be hidden. It follows the menu keyboard pattern: arrow keys move, Space and Enter toggle, Escape closes.
DataTableFacetedFilter props
| Prop | Type | Default | Description |
|---|---|---|---|
title | string | – | Label of the filter button. Required. |
facets | Record<string, number> | {} | Option key to result count. Options are sorted by count, highest first. |
labelMap | Record<string, string> | {} | Maps option keys to display labels. |
selected | string[] | [] | Selected option keys. |
onSelectionChange | (values: string[]) => void | – | Called with the new selection. Required. |