|
| 1 | +import { |
| 2 | + createColumnHelper, |
| 3 | + createPaginatedRowModel, |
| 4 | + createSortedRowModel, |
| 5 | + flexRender, |
| 6 | + rowPaginationFeature, |
| 7 | + rowSortingFeature, |
| 8 | + sortFns, |
| 9 | + tableFeatures, |
| 10 | + createTable, |
| 11 | +} from '@tanstack/solid-table' |
| 12 | +import { For, createSignal } from 'solid-js' |
| 13 | +import { makeData } from './makeData' |
| 14 | +import type { PaginationState, SortingState } from '@tanstack/solid-table' |
| 15 | +import type { Person } from './makeData' |
| 16 | + |
| 17 | +const _features = tableFeatures({ |
| 18 | + rowPaginationFeature, |
| 19 | + rowSortingFeature, |
| 20 | +}) |
| 21 | + |
| 22 | +const columnHelper = createColumnHelper<typeof _features, Person>() |
| 23 | + |
| 24 | +const columns = columnHelper.columns([ |
| 25 | + columnHelper.accessor('firstName', { |
| 26 | + header: 'First Name', |
| 27 | + cell: (info) => info.getValue(), |
| 28 | + }), |
| 29 | + columnHelper.accessor('lastName', { |
| 30 | + header: 'Last Name', |
| 31 | + cell: (info) => info.getValue(), |
| 32 | + }), |
| 33 | + columnHelper.accessor('age', { |
| 34 | + header: 'Age', |
| 35 | + }), |
| 36 | + columnHelper.accessor('visits', { |
| 37 | + header: 'Visits', |
| 38 | + }), |
| 39 | + columnHelper.accessor('status', { |
| 40 | + header: 'Status', |
| 41 | + }), |
| 42 | + columnHelper.accessor('progress', { |
| 43 | + header: 'Profile Progress', |
| 44 | + }), |
| 45 | +]) |
| 46 | + |
| 47 | +function App() { |
| 48 | + const [data] = createSignal(makeData(1000)) |
| 49 | + const [sorting, setSorting] = createSignal<SortingState>([]) |
| 50 | + const [pagination, setPagination] = createSignal<PaginationState>({ |
| 51 | + pageIndex: 0, |
| 52 | + pageSize: 10, |
| 53 | + }) |
| 54 | + |
| 55 | + const table = createTable({ |
| 56 | + _features, |
| 57 | + _rowModels: { |
| 58 | + sortedRowModel: createSortedRowModel(sortFns), |
| 59 | + paginatedRowModel: createPaginatedRowModel(), |
| 60 | + }, |
| 61 | + columns, |
| 62 | + get data() { |
| 63 | + return data() |
| 64 | + }, |
| 65 | + state: { |
| 66 | + get sorting() { |
| 67 | + return sorting() |
| 68 | + }, |
| 69 | + get pagination() { |
| 70 | + return pagination() |
| 71 | + }, |
| 72 | + }, |
| 73 | + onSortingChange: setSorting, |
| 74 | + onPaginationChange: setPagination, |
| 75 | + }) |
| 76 | + |
| 77 | + return ( |
| 78 | + <div class="p-2"> |
| 79 | + <table> |
| 80 | + <thead> |
| 81 | + <For each={table.getHeaderGroups()}> |
| 82 | + {(headerGroup) => ( |
| 83 | + <tr> |
| 84 | + <For each={headerGroup.headers}> |
| 85 | + {(header) => ( |
| 86 | + <th colSpan={header.colSpan}> |
| 87 | + {header.isPlaceholder ? null : ( |
| 88 | + <div |
| 89 | + class={ |
| 90 | + header.column.getCanSort() |
| 91 | + ? 'cursor-pointer select-none' |
| 92 | + : '' |
| 93 | + } |
| 94 | + onClick={header.column.getToggleSortingHandler()} |
| 95 | + > |
| 96 | + {flexRender( |
| 97 | + header.column.columnDef.header, |
| 98 | + header.getContext(), |
| 99 | + )} |
| 100 | + {{ |
| 101 | + asc: ' 🔼', |
| 102 | + desc: ' 🔽', |
| 103 | + }[header.column.getIsSorted() as string] ?? null} |
| 104 | + </div> |
| 105 | + )} |
| 106 | + </th> |
| 107 | + )} |
| 108 | + </For> |
| 109 | + </tr> |
| 110 | + )} |
| 111 | + </For> |
| 112 | + </thead> |
| 113 | + <tbody> |
| 114 | + <For each={table.getRowModel().rows}> |
| 115 | + {(row) => ( |
| 116 | + <tr> |
| 117 | + <For each={row.getAllCells()}> |
| 118 | + {(cell) => ( |
| 119 | + <td> |
| 120 | + {flexRender( |
| 121 | + cell.column.columnDef.cell, |
| 122 | + cell.getContext(), |
| 123 | + )} |
| 124 | + </td> |
| 125 | + )} |
| 126 | + </For> |
| 127 | + </tr> |
| 128 | + )} |
| 129 | + </For> |
| 130 | + </tbody> |
| 131 | + </table> |
| 132 | + <div class="h-2" /> |
| 133 | + <div class="flex items-center gap-2"> |
| 134 | + <button |
| 135 | + class="border rounded p-1" |
| 136 | + onClick={() => table.setPageIndex(0)} |
| 137 | + disabled={!table.getCanPreviousPage()} |
| 138 | + > |
| 139 | + {'<<'} |
| 140 | + </button> |
| 141 | + <button |
| 142 | + class="border rounded p-1" |
| 143 | + onClick={() => table.previousPage()} |
| 144 | + disabled={!table.getCanPreviousPage()} |
| 145 | + > |
| 146 | + {'<'} |
| 147 | + </button> |
| 148 | + <button |
| 149 | + class="border rounded p-1" |
| 150 | + onClick={() => table.nextPage()} |
| 151 | + disabled={!table.getCanNextPage()} |
| 152 | + > |
| 153 | + {'>'} |
| 154 | + </button> |
| 155 | + <button |
| 156 | + class="border rounded p-1" |
| 157 | + onClick={() => table.setPageIndex(table.getPageCount() - 1)} |
| 158 | + disabled={!table.getCanNextPage()} |
| 159 | + > |
| 160 | + {'>>'} |
| 161 | + </button> |
| 162 | + <span class="flex items-center gap-1"> |
| 163 | + <div>Page</div> |
| 164 | + <strong> |
| 165 | + {pagination().pageIndex + 1} of {table.getPageCount()} |
| 166 | + </strong> |
| 167 | + </span> |
| 168 | + <span class="flex items-center gap-1"> |
| 169 | + | Go to page: |
| 170 | + <input |
| 171 | + type="number" |
| 172 | + min="1" |
| 173 | + max={table.getPageCount()} |
| 174 | + value={pagination().pageIndex + 1} |
| 175 | + onInput={(e) => { |
| 176 | + const page = e.currentTarget.value |
| 177 | + ? Number(e.currentTarget.value) - 1 |
| 178 | + : 0 |
| 179 | + table.setPageIndex(page) |
| 180 | + }} |
| 181 | + class="border p-1 rounded w-16" |
| 182 | + /> |
| 183 | + </span> |
| 184 | + <select |
| 185 | + value={pagination().pageSize} |
| 186 | + onChange={(e) => { |
| 187 | + table.setPageSize(Number(e.currentTarget.value)) |
| 188 | + }} |
| 189 | + > |
| 190 | + {[10, 20, 30, 40, 50].map((pageSize) => ( |
| 191 | + <option value={pageSize}>Show {pageSize}</option> |
| 192 | + ))} |
| 193 | + </select> |
| 194 | + </div> |
| 195 | + <div class="h-4" /> |
| 196 | + <pre> |
| 197 | + {JSON.stringify( |
| 198 | + { sorting: sorting(), pagination: pagination() }, |
| 199 | + null, |
| 200 | + 2, |
| 201 | + )} |
| 202 | + </pre> |
| 203 | + </div> |
| 204 | + ) |
| 205 | +} |
| 206 | + |
| 207 | +export default App |
0 commit comments