|
| 1 | +import { |
| 2 | + columnFacetingFeature, |
| 3 | + columnFilteringFeature, |
| 4 | + createFacetedMinMaxValues, |
| 5 | + createFacetedRowModel, |
| 6 | + createFacetedUniqueValues, |
| 7 | + createFilteredRowModel, |
| 8 | + createTable, |
| 9 | + flexRender, |
| 10 | + globalFilteringFeature, |
| 11 | + tableFeatures, |
| 12 | +} from '@tanstack/solid-table' |
| 13 | +import { debounce } from '@solid-primitives/scheduled' |
| 14 | +import { For, createSignal } from 'solid-js' |
| 15 | +import { makeData } from './makeData' |
| 16 | +import ColumnFilter from './ColumnFilter' |
| 17 | +import type { Person } from './makeData' |
| 18 | +import type { ColumnDef, ColumnFiltersState } from '@tanstack/solid-table' |
| 19 | + |
| 20 | +const _features = tableFeatures({ |
| 21 | + columnFilteringFeature, |
| 22 | + globalFilteringFeature, |
| 23 | + columnFacetingFeature, |
| 24 | +}) |
| 25 | + |
| 26 | +const columns: Array<ColumnDef<typeof _features, Person>> = [ |
| 27 | + { |
| 28 | + header: 'Name', |
| 29 | + footer: (props) => props.column.id, |
| 30 | + columns: [ |
| 31 | + { |
| 32 | + accessorKey: 'firstName', |
| 33 | + cell: (info) => info.getValue(), |
| 34 | + footer: (props) => props.column.id, |
| 35 | + }, |
| 36 | + { |
| 37 | + accessorFn: (row) => row.lastName, |
| 38 | + id: 'lastName', |
| 39 | + cell: (info) => info.getValue(), |
| 40 | + header: () => <span>Last Name</span>, |
| 41 | + footer: (props) => props.column.id, |
| 42 | + }, |
| 43 | + ], |
| 44 | + }, |
| 45 | + { |
| 46 | + header: 'Info', |
| 47 | + footer: (props) => props.column.id, |
| 48 | + columns: [ |
| 49 | + { |
| 50 | + accessorKey: 'age', |
| 51 | + header: () => 'Age', |
| 52 | + footer: (props) => props.column.id, |
| 53 | + }, |
| 54 | + { |
| 55 | + header: 'More Info', |
| 56 | + columns: [ |
| 57 | + { |
| 58 | + accessorKey: 'visits', |
| 59 | + header: () => <span>Visits</span>, |
| 60 | + footer: (props) => props.column.id, |
| 61 | + }, |
| 62 | + { |
| 63 | + accessorKey: 'status', |
| 64 | + header: 'Status', |
| 65 | + footer: (props) => props.column.id, |
| 66 | + }, |
| 67 | + { |
| 68 | + accessorKey: 'progress', |
| 69 | + header: 'Profile Progress', |
| 70 | + footer: (props) => props.column.id, |
| 71 | + }, |
| 72 | + ], |
| 73 | + }, |
| 74 | + ], |
| 75 | + }, |
| 76 | +] |
| 77 | + |
| 78 | +function App() { |
| 79 | + const [data, setData] = createSignal(makeData(1_000)) |
| 80 | + const [columnFilters, setColumnFilters] = createSignal<ColumnFiltersState>([]) |
| 81 | + const [globalFilter, setGlobalFilter] = createSignal('') |
| 82 | + const debounceSetGlobalFilter = debounce( |
| 83 | + (value: string) => setGlobalFilter(value), |
| 84 | + 500, |
| 85 | + ) |
| 86 | + const refreshData = () => setData(makeData(50_000)) // stress test |
| 87 | + |
| 88 | + const table = createTable({ |
| 89 | + _features, |
| 90 | + _rowModels: { |
| 91 | + facetedRowModel: createFacetedRowModel(), |
| 92 | + facetedMinMaxValues: createFacetedMinMaxValues(), |
| 93 | + facetedUniqueValues: createFacetedUniqueValues(), |
| 94 | + filteredRowModel: createFilteredRowModel(), |
| 95 | + }, |
| 96 | + get data() { |
| 97 | + return data() |
| 98 | + }, |
| 99 | + columns, |
| 100 | + state: { |
| 101 | + get columnFilters() { |
| 102 | + return columnFilters() |
| 103 | + }, |
| 104 | + get globalFilter() { |
| 105 | + return globalFilter() |
| 106 | + }, |
| 107 | + }, |
| 108 | + onGlobalFilterChange: setGlobalFilter, |
| 109 | + globalFilterFn: 'includesString', |
| 110 | + onColumnFiltersChange: setColumnFilters, |
| 111 | + debugTable: true, |
| 112 | + debugHeaders: true, |
| 113 | + debugColumns: false, |
| 114 | + }) |
| 115 | + |
| 116 | + return ( |
| 117 | + <div class="p-2"> |
| 118 | + <input |
| 119 | + class="p-2 font-lg shadow border border-block" |
| 120 | + value={globalFilter()} |
| 121 | + onInput={(e) => debounceSetGlobalFilter(e.currentTarget.value)} |
| 122 | + placeholder="Search all columns..." |
| 123 | + /> |
| 124 | + <div class="h-2" /> |
| 125 | + <table> |
| 126 | + <thead> |
| 127 | + <For each={table.getHeaderGroups()}> |
| 128 | + {(headerGroup) => ( |
| 129 | + <tr> |
| 130 | + <For each={headerGroup.headers}> |
| 131 | + {(header) => ( |
| 132 | + <th colSpan={header.colSpan}> |
| 133 | + {header.isPlaceholder ? null : ( |
| 134 | + <> |
| 135 | + {flexRender( |
| 136 | + header.column.columnDef.header, |
| 137 | + header.getContext(), |
| 138 | + )} |
| 139 | + {header.column.getCanFilter() ? ( |
| 140 | + <div> |
| 141 | + <ColumnFilter |
| 142 | + column={header.column} |
| 143 | + table={table as any} |
| 144 | + /> |
| 145 | + </div> |
| 146 | + ) : null} |
| 147 | + </> |
| 148 | + )} |
| 149 | + </th> |
| 150 | + )} |
| 151 | + </For> |
| 152 | + </tr> |
| 153 | + )} |
| 154 | + </For> |
| 155 | + </thead> |
| 156 | + <tbody> |
| 157 | + <For each={table.getRowModel().rows.slice(0, 10)}> |
| 158 | + {(row) => ( |
| 159 | + <tr> |
| 160 | + <For each={row.getVisibleCells()}> |
| 161 | + {(cell) => ( |
| 162 | + <td> |
| 163 | + {flexRender( |
| 164 | + cell.column.columnDef.cell, |
| 165 | + cell.getContext(), |
| 166 | + )} |
| 167 | + </td> |
| 168 | + )} |
| 169 | + </For> |
| 170 | + </tr> |
| 171 | + )} |
| 172 | + </For> |
| 173 | + </tbody> |
| 174 | + </table> |
| 175 | + <div>{table.getRowModel().rows.length} Rows</div> |
| 176 | + <div> |
| 177 | + <button onClick={() => refreshData()}>Refresh Data</button> |
| 178 | + </div> |
| 179 | + <pre>{JSON.stringify(table.getState(), null, 2)}</pre> |
| 180 | + </div> |
| 181 | + ) |
| 182 | +} |
| 183 | + |
| 184 | +export default App |
0 commit comments