|
| 1 | +import { createTableHook } from '@tanstack/solid-table' |
| 2 | +import { For, createSignal } from 'solid-js' |
| 3 | + |
| 4 | +// This example uses the new `createTableHook` method to create a re-usable table hook factory instead of independently using the standalone `useTable` hook and `createColumnHelper` method. You can choose to use either way. |
| 5 | + |
| 6 | +// 1. Define what the shape of your data will be for each row |
| 7 | +type Person = { |
| 8 | + firstName: string |
| 9 | + lastName: string |
| 10 | + age: number |
| 11 | + visits: number |
| 12 | + status: string |
| 13 | + progress: number |
| 14 | +} |
| 15 | + |
| 16 | +// 2. Create some dummy data with a stable reference (this could be an API response stored in useState or similar) |
| 17 | +const defaultData: Array<Person> = [ |
| 18 | + { |
| 19 | + firstName: 'tanner', |
| 20 | + lastName: 'linsley', |
| 21 | + age: 24, |
| 22 | + visits: 100, |
| 23 | + status: 'In Relationship', |
| 24 | + progress: 50, |
| 25 | + }, |
| 26 | + { |
| 27 | + firstName: 'tandy', |
| 28 | + lastName: 'miller', |
| 29 | + age: 40, |
| 30 | + visits: 40, |
| 31 | + status: 'Single', |
| 32 | + progress: 80, |
| 33 | + }, |
| 34 | + { |
| 35 | + firstName: 'joe', |
| 36 | + lastName: 'dirte', |
| 37 | + age: 45, |
| 38 | + visits: 20, |
| 39 | + status: 'Complicated', |
| 40 | + progress: 10, |
| 41 | + }, |
| 42 | + { |
| 43 | + firstName: 'kevin', |
| 44 | + lastName: 'vandy', |
| 45 | + age: 28, |
| 46 | + visits: 100, |
| 47 | + status: 'Single', |
| 48 | + progress: 70, |
| 49 | + }, |
| 50 | +] |
| 51 | + |
| 52 | +// 3. New in V9! Tell the table which features and row models we want to use. In this case, this will be a basic table with no additional features |
| 53 | +const { createAppTable, createAppColumnHelper } = createTableHook({ |
| 54 | + _features: {}, |
| 55 | + _rowModels: {}, // client-side row models. `Core` row model is now included by default, but you can still override it here |
| 56 | + debugTable: true, |
| 57 | +}) |
| 58 | + |
| 59 | +// 4. Create a helper object to help define our columns |
| 60 | +const columnHelper = createAppColumnHelper<Person>() |
| 61 | + |
| 62 | +// 5. Define the columns for your table with a stable reference (in this case, defined statically outside of a react component) |
| 63 | +const columns = columnHelper.columns([ |
| 64 | + // accessorKey method (most common for simple use-cases) |
| 65 | + columnHelper.accessor('firstName', { |
| 66 | + cell: (info) => info.getValue(), |
| 67 | + footer: (info) => info.column.id, |
| 68 | + }), |
| 69 | + // accessorFn used (alternative) along with a custom id |
| 70 | + columnHelper.accessor((row) => row.lastName, { |
| 71 | + id: 'lastName', |
| 72 | + cell: (info) => <i>{info.getValue()}</i>, |
| 73 | + header: () => <span>Last Name</span>, |
| 74 | + footer: (info) => info.column.id, |
| 75 | + }), |
| 76 | + // accessorFn used to transform the data |
| 77 | + columnHelper.accessor((row) => Number(row.age), { |
| 78 | + id: 'age', |
| 79 | + header: () => 'Age', |
| 80 | + cell: (info) => info.renderValue(), |
| 81 | + footer: (info) => info.column.id, |
| 82 | + }), |
| 83 | + columnHelper.accessor('visits', { |
| 84 | + header: () => <span>Visits</span>, |
| 85 | + footer: (info) => info.column.id, |
| 86 | + }), |
| 87 | + columnHelper.accessor('status', { |
| 88 | + header: 'Status', |
| 89 | + footer: (info) => info.column.id, |
| 90 | + }), |
| 91 | + columnHelper.accessor('progress', { |
| 92 | + header: 'Profile Progress', |
| 93 | + footer: (info) => info.column.id, |
| 94 | + }), |
| 95 | +]) |
| 96 | + |
| 97 | +export function App() { |
| 98 | + // 6. Store data with a stable reference |
| 99 | + const [data, setData] = createSignal([...defaultData]) |
| 100 | + |
| 101 | + // Helper to rerender with sorted data (by age ascending) |
| 102 | + function rerender() { |
| 103 | + setData((prev) => |
| 104 | + prev.slice().sort((a: Person, b: Person) => a.age - b.age), |
| 105 | + ) |
| 106 | + } |
| 107 | + |
| 108 | + // 7. Create the table instance with the required columns and data. |
| 109 | + // Features and row models are already defined in the createTableHook call above |
| 110 | + const table = createAppTable({ |
| 111 | + columns, |
| 112 | + get data() { |
| 113 | + return data() |
| 114 | + }, |
| 115 | + // add additional table options here or in the createTableHook call above |
| 116 | + }) |
| 117 | + |
| 118 | + // 8. Render your table markup from the table instance APIs |
| 119 | + return ( |
| 120 | + <div class="p-2"> |
| 121 | + <table> |
| 122 | + <thead> |
| 123 | + <For each={table.getHeaderGroups()}> |
| 124 | + {(headerGroup) => ( |
| 125 | + <tr> |
| 126 | + <For each={headerGroup.headers}> |
| 127 | + {(header) => ( |
| 128 | + <th> |
| 129 | + <table.FlexRender header={header} /> |
| 130 | + </th> |
| 131 | + )} |
| 132 | + </For> |
| 133 | + </tr> |
| 134 | + )} |
| 135 | + </For> |
| 136 | + </thead> |
| 137 | + <tbody> |
| 138 | + <For each={table.getRowModel().rows}> |
| 139 | + {(row) => ( |
| 140 | + <tr> |
| 141 | + <For each={row.getAllCells()}> |
| 142 | + {(cell) => ( |
| 143 | + <td> |
| 144 | + <table.FlexRender cell={cell} /> |
| 145 | + </td> |
| 146 | + )} |
| 147 | + </For> |
| 148 | + </tr> |
| 149 | + )} |
| 150 | + </For> |
| 151 | + </tbody> |
| 152 | + <tfoot> |
| 153 | + <For each={table.getFooterGroups()}> |
| 154 | + {(footerGroup) => ( |
| 155 | + <tr> |
| 156 | + <For each={footerGroup.headers}> |
| 157 | + {(header) => ( |
| 158 | + <th> |
| 159 | + <table.FlexRender footer={header} /> |
| 160 | + </th> |
| 161 | + )} |
| 162 | + </For> |
| 163 | + </tr> |
| 164 | + )} |
| 165 | + </For> |
| 166 | + </tfoot> |
| 167 | + </table> |
| 168 | + <div class="h-4" /> |
| 169 | + <button onClick={rerender} class="border p-2"> |
| 170 | + Rerender (sort by age) |
| 171 | + </button> |
| 172 | + </div> |
| 173 | + ) |
| 174 | +} |
0 commit comments