import { customElement, state } from 'lit/decorators.js' import { LitElement, html } from 'lit' import { repeat } from 'lit/directives/repeat.js' import { createSortedRowModel, createTableHook, rowSortingFeature, sortFn_alphanumeric, sortFn_text, tableFeatures, } from '@tanstack/lit-table' import { makeData } from './makeData' import type { Person } from './makeData' // This example uses the new `createTableHook` method to create a re-usable table hook factory instead of independently using the standalone `TableController` and `createColumnHelper` method. You can choose to use either way. // 3. New in V9! Tell the table which features and row models we want to use. In this case, we want sorting. const { useAppTable, createAppColumnHelper } = createTableHook({ features: tableFeatures({ rowSortingFeature, sortedRowModel: createSortedRowModel(), sortFns: { alphanumeric: sortFn_alphanumeric, text: sortFn_text, }, }), debugTable: true, }) // 4. Create a helper object to help define our columns const columnHelper = createAppColumnHelper() // 5. Define the columns for your table with a stable reference (in this case, defined statically outside of a Lit component) const columns = columnHelper.columns([ // accessorKey method (most common for simple use-cases) columnHelper.accessor('firstName', { cell: (info) => info.getValue(), footer: (info) => info.column.id, }), // accessorFn used (alternative) along with a custom id columnHelper.accessor((row) => row.lastName, { id: 'lastName', cell: (info) => html`${info.getValue()}`, header: () => html`Last Name`, footer: (info) => info.column.id, }), // accessorFn used to transform the data columnHelper.accessor((row) => Number(row.age), { id: 'age', header: () => 'Age', cell: (info) => info.renderValue(), footer: (info) => info.column.id, }), columnHelper.accessor('visits', { header: () => html`Visits`, footer: (info) => info.column.id, }), columnHelper.accessor('status', { header: 'Status', footer: (info) => info.column.id, }), columnHelper.accessor('progress', { header: 'Profile Progress', footer: (info) => info.column.id, }), ]) @customElement('lit-table-example') class LitTableExample extends LitElement { @state() private data: Array = makeData(20) // 6. Store data with a reactive reference // (in Lit, we use @state() for reactive properties) // 7. Create the table instance with the required columns and data. // Features and row models are already defined in the createTableHook call above // NOTE: Capture `this` as `host` because inside the getter, `this` refers // to the options object (not the LitElement), which would cause infinite recursion. private appTable = (() => { const host = this return useAppTable( this, { debugTable: true, columns, get data() { return host.data }, }, (state) => ({ sorting: state.sorting }), ) })() // 8. Render your table markup from the table instance APIs protected render(): unknown { const table = this.appTable.table() return html`
${repeat( table.getHeaderGroups(), (headerGroup) => headerGroup.id, (headerGroup) => html` ${headerGroup.headers.map((header) => table.AppHeader( header, (h) => html` `, ), )} `, )} ${table.getRowModel().rows.map( (row) => html` ${row .getAllCells() .map((cell) => table.AppCell( cell, (c) => html` `, ), )} `, )} ${repeat( table.getFooterGroups(), (footerGroup) => footerGroup.id, (footerGroup) => html` ${footerGroup.headers.map((header) => table.AppFooter( header, (h) => html` `, ), )} `, )}
${ h.isPlaceholder ? null : html`
${h.FlexRender()} ${ { asc: ' \u{1F53C}', desc: ' \u{1F53D}' }[ h.column.getIsSorted() as string ] ?? null }
` }
${c.FlexRender()}
${h.isPlaceholder ? null : h.FlexRender()}
${JSON.stringify(table.state, null, 2)}
` } }