import { customElement, state } from 'lit/decorators.js' import { LitElement, html } from 'lit' import { repeat } from 'lit/directives/repeat.js' import { FlexRender, TableController, createColumnHelper, createPaginatedRowModel, createSortedRowModel, rowPaginationFeature, rowSortingFeature, sortFn_alphanumeric, sortFn_text, tableFeatures, } from '@tanstack/lit-table' import { makeData } from './makeData' import type { PaginationState, SortingState } from '@tanstack/lit-table' import type { Person } from './makeData' // This example demonstrates managing table state externally via Lit's @state() decorator instead of letting the table manage its own state internally. const features = tableFeatures({ rowPaginationFeature, rowSortingFeature, sortedRowModel: createSortedRowModel(), paginatedRowModel: createPaginatedRowModel(), sortFns: { alphanumeric: sortFn_alphanumeric, text: sortFn_text, }, }) const columnHelper = createColumnHelper() const columns = columnHelper.columns([ columnHelper.accessor('firstName', { header: 'First Name', cell: (info) => info.getValue(), }), columnHelper.accessor('lastName', { header: 'Last Name', cell: (info) => info.getValue(), }), columnHelper.accessor('age', { header: 'Age', }), columnHelper.accessor('visits', { header: 'Visits', }), columnHelper.accessor('status', { header: 'Status', }), columnHelper.accessor('progress', { header: 'Profile Progress', }), ]) @customElement('lit-table-example') class LitTableExample extends LitElement { @state() private _data: Array = makeData(1_000) private tableController = new TableController(this) // Manage sorting state with Lit's @state() decorator @state() private sorting: SortingState = [] // Manage pagination state with Lit's @state() decorator @state() private pagination: PaginationState = { pageIndex: 0, pageSize: 10 } protected render() { // Create the table and pass state + onChange handlers const table = this.tableController.table( { features, columns, data: this._data, state: { sorting: this.sorting, // connect our sorting state back down to the table pagination: this.pagination, // connect our pagination state back down to the table }, onSortingChange: (updater) => { // raise sorting state changes to our own state management this.sorting = typeof updater === 'function' ? updater(this.sorting) : updater }, onPaginationChange: (updater) => { // raise pagination state changes to our own state management this.pagination = typeof updater === 'function' ? updater(this.pagination) : updater }, }, (state) => ({ sorting: state.sorting, pagination: state.pagination, }), ) return html`
${repeat( table.getHeaderGroups(), (headerGroup) => headerGroup.id, (headerGroup) => html` ${repeat( headerGroup.headers, (header) => header.id, (header) => html` `, )} `, )} ${repeat( table.getRowModel().rows, (row) => row.id, (row) => html` ${repeat( row.getAllCells(), (cell) => cell.id, (cell) => html` `, )} `, )}
${ header.isPlaceholder ? null : html`
${FlexRender({ header })} ${ { asc: ' 🔼', desc: ' 🔽', }[header.column.getIsSorted() as string] ?? null }
` }
${FlexRender({ cell })}
Page
${(this.pagination.pageIndex + 1).toLocaleString()} of ${table.getPageCount().toLocaleString()}
| Go to page:
${JSON.stringify(table.state, null, 2)}
` } }