Skip to main content

Function: tanstackTable()

tanstackTable<TFeatures, TData>(spec, rows, o): SdvTanstackTable<TFeatures, TData>

Defined in: packages/sdvtables/src/tanstack-table.ts:137

An sdvtables spec as TanStack Table v9 options: useTable(tanstackTable(spec, rows, { features }).options).

  • Columns: one accessor column per spec column, id the spec's key, header the HTML table's label (with a scaleNote's suffix). Number cells (num, int, pct, rank, a scaleNote's divisor and decimals) are the HTML table's text; a blank or unparseable number is missing (undefined), never 0.
  • Sorting: sdvtables' own comparator (the column's compare, else by kind), with missing values last in both directions (sortUndefined: "last"). Register rowSortingFeature and createSortedRowModel() to use it.
  • Row ids: with the spec's rowKey, String(row[rowKey]), checked unique and not blank (TanStack would merge two rows with one id); without it, TanStack's row index.
  • Images: logo, wordmark and headshot cells, and superscript ranks, go through render as a CellPart. A team or player with no image keeps its text, with one warning. Team values may be abbreviations, names or numeric ids.

Other kinds (pills, bars, team colours, sparklines...) show their plain value, with one warning per kind. Synchronous: call await prepare(spec) (@sportsdataverse/sdvtables/html) first. Images are worked out once, from rows: build again when the rows change, and keep the result stable between renders (module scope or a memo).

Type Parameters​

TFeatures​

TFeatures extends TableFeatures

TData​

TData extends RowData

Parameters​

spec​

TableSpec<TData>

rows​

readonly TData[]

o​

TanstackTableOptions<TFeatures>

Returns​

SdvTanstackTable<TFeatures, TData>

Example​

import { createSortedRowModel, rowSortingFeature, tableFeatures } from "@tanstack/table-core";
import { defineTable } from "@sportsdataverse/sdvtables";
import { prepare } from "@sportsdataverse/sdvtables/html";
import { tanstackTable } from "@sportsdataverse/sdvtables/tanstack-table";

// 2024 regular season, nflverse games
const rows = [
{ team: "KC", wins: 15, pf: 385 },
{ team: "BUF", wins: 13, pf: 525 },
{ team: "LAC", wins: 11, pf: 402 },
];
const spec = defineTable<(typeof rows)[number]>()
.columns((c) => [c.logo("team", { league: "nfl", height: 28 }), c.int("wins"), c.int("pf")])
.rowKey("team")
.build();
await prepare(spec);
const features = tableFeatures({ rowSortingFeature, sortedRowModel: createSortedRowModel() });
const { options } = tanstackTable(spec, rows, { features });
// in React: useTable(options), from @tanstack/react-table; in Svelte: createTable(options)
options.columns.map((c) => [c.id, c.header]);