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