Data Table

Styled, accessible parts for data tables powered by TanStack Table: sorting, filtering, column visibility, row selection, pinning and pagination.

Data DisplayExperimental

Installation

It has its own entry point, @semantic-components/ui/data-table, built on @tanstack/angular-table — an optional peer of ui: install it where you use the component.

ts
import { ScDataTableProvider, ScDataTable, ScDataTableRow, ScDataTableCell } from '@semantic-components/ui/data-table';

Usage

import {  createColumnHelper,  createSortedRowModel,  injectTable,  rowSortingFeature,  sortFn_alphanumeric,  tableFeatures,} from '@tanstack/angular-table';import { Component, ViewEncapsulation } from '@angular/core';import { ScFrame } from '@semantic-components/ui';import {  ScDataTable,  ScDataTableBody,  ScDataTableCell,  ScDataTableContainer,  ScDataTableHeader,  ScDataTableHeaderCell,  ScDataTableRow,  ScDataTableSortButton,} from '@semantic-components/ui/data-table';import {  SiArrowDownIcon,  SiArrowUpIcon,  SiChevronsUpDownIcon,} from '@semantic-icons/lucide-icons';
interface Payment {  id: string;  status: 'pending' | 'processing' | 'success' | 'failed';  email: string;  amount: number;}
/** TanStack's features the table uses: sorting. */const features = tableFeatures({  rowSortingFeature,  sortedRowModel: createSortedRowModel(),  sortFns: { alphanumeric: sortFn_alphanumeric },});
const columnHelper = createColumnHelper<typeof features, Payment>();
const columns = columnHelper.columns([  columnHelper.accessor('status', { header: 'Status', enableSorting: false }),  columnHelper.accessor('email', { header: 'Email' }),  columnHelper.accessor('amount', { header: 'Amount', enableSorting: false }),]);
/** TanStack Table holds the rows and their order; the parts render it — here sorted by email. */@Component({  selector: 'app-data-table-usage-demo',  imports: [    ScFrame,    ScDataTableCell,    ScDataTableHeaderCell,    ScDataTableRow,    ScDataTableSortButton,    SiArrowDownIcon,    SiArrowUpIcon,    SiChevronsUpDownIcon,    ScDataTableContainer,    ScDataTable,    ScDataTableHeader,    ScDataTableBody,  ],  template: `    <div scDataTableContainer scFrame class="w-full max-w-lg">      <table scDataTable>        <thead scDataTableHeader>          @for (headerGroup of table.getHeaderGroups(); track headerGroup.id) {            <tr scDataTableRow>              @for (header of headerGroup.headers; track header.id) {                <th scDataTableHeaderCell [column]="header.column">                  @if (header.column.getCanSort()) {                    <button                      scDataTableSortButton                      #sort="scDataTableSortButton"                      [column]="header.column"                    >                      {{ header.column.columnDef.header }}                      @switch (sort.direction()) {                        @case ('asc') {                          <svg siArrowUpIcon></svg>                        }                        @case ('desc') {                          <svg siArrowDownIcon></svg>                        }                        @default {                          <svg siChevronsUpDownIcon></svg>                        }                      }                    </button>                  } @else {                    <div [class.text-end]="header.column.id === 'amount'">                      {{ header.column.columnDef.header }}                    </div>                  }                </th>              }            </tr>          }        </thead>        <tbody scDataTableBody>          @for (row of table.getRowModel().rows; track row.id) {            <tr scDataTableRow [row]="row">              <td scDataTableCell class="capitalize">                {{ row.original.status }}              </td>              <td scDataTableCell>{{ row.original.email }}</td>              <td scDataTableCell class="text-end font-medium">                {{ amount.format(row.original.amount) }}              </td>            </tr>          }        </tbody>      </table>    </div>  `,  host: { class: 'flex w-full justify-center' },  encapsulation: ViewEncapsulation.None,})export class DataTableUsageDemo {  private readonly amount = new Intl.NumberFormat('en-US', {    style: 'currency',    currency: 'USD',  });
  private readonly table = injectTable(() => ({    features,    columns,    data: PAYMENTS,    getRowId: (payment) => payment.id,  }));}
const PAYMENTS: Payment[] = [  {    id: 'm5gr84i9',    amount: 316,    status: 'success',    email: '[email protected]',  },  {    id: '3u1reuv4',    amount: 242,    status: 'success',    email: '[email protected]',  },  {    id: 'derv1ws0',    amount: 837,    status: 'processing',    email: '[email protected]',  },  {    id: '5kma53ae',    amount: 874,    status: 'success',    email: '[email protected]',  },  {    id: 'bhqecj4p',    amount: 721,    status: 'failed',    email: '[email protected]',  },];

Anatomy

TanStack Table builds the table (its columns, rows and state); the data table's parts render it, one attribute per element: each brings the look of the part it stands for (the Table's, the Input's, the Pagination's). The checkboxes are the exception: they go on an scCheckbox. The provider holds the table once, for the parts that need it: the filter, "select all", the pagination, a tree grid. Add the parts for what you need: sorting, filtering, selection, paging, column visibility, a tree.

import {  ScDataTableProvider, ScDataTableContainer, ScDataTable, ScDataTableHeader,  ScDataTableBody, ScDataTableRow, ScDataTableHeaderCell, ScDataTableCell, ScDataTableSortButton,  ScDataTableSelectAll, ScDataTableSelectRow, ScDataTableGlobalFilter,  ScDataTableColumnFilter, ScDataTableColumnVisibility, ScDataTablePagination,} from '@semantic-components/ui/data-table';
<div scDataTableProvider [table]="table">  <input scDataTableGlobalFilter aria-label="…" />  <div scDataTableContainer>    <table scDataTable>      <thead scDataTableHeader>        <tr scDataTableRow>          <th scDataTableHeaderCell [column]="…">            <input type="checkbox" scCheckbox scDataTableSelectAll aria-label="…" />            <button scDataTableSortButton [column]="…">…</button>          </th>        </tr>      </thead>      <tbody scDataTableBody>        <tr scDataTableRow [row]="row">          <td scDataTableCell [column]="…">            <input type="checkbox" scCheckbox scDataTableSelectRow [row]="row" />          </td>        </tr>      </tbody>    </table>  </div>  <nav scDataTablePagination aria-label="…">…</nav></div>

TanStack Table

TanStack Table keeps the state — sorting, filters, selection, the page, the columns shown and pinned — and these parts render it and send your clicks back. Register the features of the parts you use with tableFeatures(): TanStack only gives a table the methods of its features. The parts are typed structurally, so they don't pin a TanStack version.

  • Sorting: rowSortingFeature and a sorted row model, the sort button in the header cell.
  • Filtering: globalFilteringFeature or columnFilteringFeature, a filtered row model and the filter functions you name (filterFns: { includesString }, a column's filterFn: 'includesString').
  • Selection: rowSelectionFeature; select all covers the page (mode="all": every row).
  • Pages: rowPaginationFeature and a paginated row model; the Pagination's parts inside scDataTablePagination.
  • Columns shown: columnVisibilityFeature — a Menu's checkbox items, or scDataTableColumnVisibility checkboxes in a Popover.
  • Pinned columns: columnPinningFeature; with several on one side, column sizing gives their offsets.

Accessibility

  • A sortable column's header says how it is sorted (aria-sort); its button sorts it, Shift + click adds it to the sort.
  • Name every checkbox ("Select all", "Select row" or the row's name), every filter, and the buttons of a row (a span with sr-only).
  • The pagination is a navigation landmark: name it.
  • A table wider than its box scrolls: make the box a named region that takes focus (tabindex="0", role="region", aria-label), so the keyboard scrolls it too — the Pinned Columns example.
  • A change the page doesn't show — a copied id — said in a live region.

Examples

Payments

Select Status
Amount
Actions
success
$316.00
success
$242.00
processing
$837.00
success
$874.00
failed
$721.00
0 of 5 row(s) selected.

import {  columnFilteringFeature,  columnVisibilityFeature,  createColumnHelper,  createFilteredRowModel,  createPaginatedRowModel,  createSortedRowModel,  filterFn_includesString,  injectTable,  rowPaginationFeature,

Full Featured

SelectActions
John Doe [email protected] Admin active Jan 15, 2024
Jane Smith [email protected] Editor active Feb 20, 2024
Bob Johnson [email protected] Viewer inactive Jan 10, 2024
Alice Brown [email protected] Editor pending Mar 5, 2024
Charlie Wilson [email protected] Admin active Feb 28, 2024

0 of 12 row(s) selected.

import {  columnFilteringFeature,  columnVisibilityFeature,  createColumnHelper,  createFilteredRowModel,  createPaginatedRowModel,  createSortedRowModel,  globalFilteringFeature,  injectTable,  rowPaginationFeature,

Pinned Columns

Name Email Role Team Office Phone Status Actions
Olivia Martin [email protected] Engineer Platform Lisbon +351 912 345 678 Active
Jackson Lee [email protected] Designer Product Toronto +1 416 555 0134 Away
Isabella Nguyen [email protected] Manager Sales Singapore +65 6123 4567 Active
William Kim [email protected] Engineer Data Seoul +82 2 555 0198 Active
Sofia Davis [email protected] Analyst Finance Berlin +49 30 5550 1234 Away
import {  columnPinningFeature,  createColumnHelper,  injectTable,  tableFeatures,} from '@tanstack/angular-table';import { Component, ViewEncapsulation } from '@angular/core';import { ScBadge, ScButton, ScFrame } from '@semantic-components/ui';import {  ScDataTable,

RTL

تحديد الحالة
ناجحة[email protected] ‏316.00 US$
ناجحة[email protected] ‏242.00 US$
قيد المعالجة[email protected] ‏837.00 US$
ناجحة[email protected] ‏874.00 US$
0 من 6 صف محدد.
import {  columnFilteringFeature,  createColumnHelper,  createFilteredRowModel,  createPaginatedRowModel,  createSortedRowModel,  filterFn_includesString,  injectTable,  rowPaginationFeature,  rowSelectionFeature,

API reference

Each part takes class, merged over its look (cn: a later class wins). The parts are typed structurally: pass TanStack's own table, column and row objects.

PartSelectorDescription
ScDataTableProviderdiv[scDataTableProvider] [table]: TanStack's table, held for the parts around it and in it — the filter, select all, the pagination, a tree grid. Wraps them.
ScDataTableContainer, ScDataTable, ScDataTableHeader, ScDataTableBody, ScDataTableFooter, ScDataTableCaptiondiv[scDataTableContainer], table[scDataTable], thead[scDataTableHeader], tbody[scDataTableBody], tfoot[scDataTableFooter], caption[scDataTableCaption] The table's box (it scrolls sideways), the table and its sections, in the Table's look.
ScDataTableHeaderCellth[scDataTableHeaderCell] A header cell, in the Table's look; bound to its column ([column]): aria-sort while sortable, sticky when pinned.
ScDataTableCelltd[scDataTableCell] A cell, in the Table's look; bound to its column ([column]): sticky when pinned.
ScDataTableRowtr[scDataTableRow] A row, in the Table's look; bound to its row ([row], in the body): data-state="selected" while selected; in a tree grid, its level, place and open state.
ScDataTableSortButtonbutton[scDataTableSortButton] [column]: sorts it on click (Shift+click adds to a multi-sort). The style's button (variant ghost, size sm by default). Your icons, from direction() (exportAs scDataTableSortButton).
ScDataTableSelectAllinput[type="checkbox"][scDataTableSelectAll] Checked when every row is selected, indeterminate when some are. mode ('page', default, or 'all'). On an scCheckbox; name it.
ScDataTableSelectRowinput[type="checkbox"][scDataTableSelectRow] [row]: the row's selection; disabled when it can't be selected, half-ticked when only some sub-rows are. On an scCheckbox; name it.
ScDataTableGlobalFilter, ScDataTableColumnFilterinput[scDataTableGlobalFilter], input[scDataTableColumnFilter] Filter the whole table or one column ([column]) as you type; empty clears it. In the Input's look; name them.
ScDataTableColumnVisibilityinput[type="checkbox"][scDataTableColumnVisibility] [column]: shows or hides it. On an scCheckbox — in a popover, say.
ScDataTablePaginationnav[scDataTablePagination], div[scDataTablePagination] The Pagination, its page, page size and row count from the table, its page changes back to it (exportAs scPagination). Compose the Pagination's parts inside as usual.
ScDataTableTreeGridtable[scDataTableTreeGrid] A tree table (WAI-ARIA treegrid), in the Table's look; its table from scDataTableProvider. One tab stop: ↑ ↓ between rows, → / ← open, close or move in and out (mirrored right to left), Home and End, Enter opens, Space selects (exportAs scDataTableTreeGrid).
ScDataTableTreeCelldiv[scDataTableTreeCell] [row]: a tree row's first cell content, indented by its depth — the expand button, then the rest, in a line.
ScDataTableExpandbutton[scDataTableExpand] [row]: opens or closes its children; variant (ghost) and size (icon-xs), the style's button. Your chevron (pointing to the end) and its name inside; unseen on a row without children. In a tree grid, out of the tab order.
ScDataTableExpandAllbutton[scDataTableExpandAll] Opens every row, or closes them all (aria-expanded); the table from scDataTableProvider; variant (outline) and size (sm), the style's button.