import { FlexRender, columnSizingFeature, createSortedRowModel, createTable, rowSelectionFeature, rowSortingFeature, sortFns, tableFeatures, } from '@tanstack/solid-table' import { createVirtualizer } from '@tanstack/solid-virtual' import { For, createEffect, createSignal } from 'solid-js' import { makeData } from './makeData' import type { Row, SolidTable } from '@tanstack/solid-table' import type { VirtualItem, Virtualizer } from '@tanstack/solid-virtual' import type { Person } from './makeData' const features = tableFeatures({ columnSizingFeature, rowSelectionFeature, rowSortingFeature, sortedRowModel: createSortedRowModel(), sortFns, }) // This is a dynamic row height example, which is more complicated, but allows for a more realistic table. // See https://tanstack.com/virtual/v3/docs/examples/solid/table for a simpler fixed row height example. function App() { const columns = [ { id: 'select', header: ({ table }: any) => ( ), cell: ({ row }: any) => ( ), size: 40, }, { accessorKey: 'id', header: 'ID', size: 60, }, { accessorKey: 'firstName', cell: (info: any) => info.getValue(), }, { accessorFn: (row: Person) => row.lastName, id: 'lastName', cell: (info: any) => info.getValue(), header: () => Last Name, }, { accessorKey: 'age', header: () => 'Age', size: 50, }, { accessorKey: 'visits', header: () => Visits, size: 50, }, { accessorKey: 'status', header: 'Status', }, { accessorKey: 'progress', header: 'Profile Progress', size: 80, }, { accessorKey: 'createdAt', header: 'Created At', cell: (info: any) => (info.getValue() as Date).toLocaleString(), size: 250, }, ] const [data, setData] = createSignal(makeData(200_000)) const refreshData = () => setData(makeData(200_000)) const stressTest = () => setData(makeData(1_000_000)) const table = createTable({ features, columns, get data() { return data() }, getRowId: (row) => String(row.id), debugTable: true, }) return ( <>
({data().length.toLocaleString()} rows)

Hold Shift while selecting rows to select or deselect a range.

{table.getSelectedRowIds().length.toLocaleString()} rows selected

) } // Important: Keep the virtualizer and the scroll container ref in the same component. // The ref must be undefined when createVirtualizer runs (before JSX return), // so that onMount can set up scroll observers after the element is in the DOM. function VirtualizedTable(props: { table: SolidTable }) { let tableContainerRef: HTMLDivElement | undefined const rows = () => props.table.getRowModel().rows // Important: Keep the row virtualizer in the lowest component possible to avoid unnecessary re-renders. const rowVirtualizer = createVirtualizer( { get count() { return rows().length }, estimateSize: () => 33, // estimate row height for accurate scrollbar dragging getScrollElement: () => tableContainerRef ?? null, // measure dynamic row height, except in firefox because it measures table border height incorrectly measureElement: typeof window !== 'undefined' && navigator.userAgent.indexOf('Firefox') === -1 ? (element) => element.getBoundingClientRect().height : undefined, overscan: 5, }, ) return (
{/* Even though we're still using semantic table tags, we must use CSS grid and flexbox for dynamic row heights */} {(headerGroup) => ( {(header) => ( )} )} {(virtualRow) => { return ( rows()[virtualRow.index]} virtualRow={virtualRow} rowVirtualizer={rowVirtualizer} table={props.table} /> ) }}
{( { asc: ' 🔼', desc: ' 🔽', } as Record )[header.column.getIsSorted() as string] ?? null}
) } function TableBodyRow(props: { row: () => Row virtualRow: VirtualItem rowVirtualizer: Virtualizer table: SolidTable }) { return ( props.rowVirtualizer.measureElement(node)} // measure dynamic row height style={{ display: 'flex', position: 'absolute', transform: `translateY(${props.virtualRow.start}px)`, // this should always be a `style` as it changes on scroll width: '100%', }} > {(cell) => ( )} ) } function IndeterminateCheckbox(props: { indeterminate?: boolean class?: string checked?: boolean disabled?: boolean onChange?: (event: Event) => void onClick?: (event: MouseEvent) => void }) { let ref: HTMLInputElement | undefined createEffect(() => { if (typeof props.indeterminate === 'boolean' && ref) { ref.indeterminate = !props.checked && props.indeterminate } }) return ( ) } export default App