import { createTable, flexRender, tableFeatures } from '@tanstack/solid-table'
import { For, createSignal } from 'solid-js'
import type { ColumnDef } from '@tanstack/solid-table'
// This example uses the classic standalone `useTable` hook to create a table without the new `createTableHelper` util.
// 1. Define what the shape of your data will be for each row
type Person = {
firstName: string
lastName: string
age: number
visits: number
status: string
progress: number
}
// 2. Create some dummy data with a stable reference (this could be an API response stored in useState or similar)
const defaultData: Array<Person> = [
{
firstName: 'tanner',
lastName: 'linsley',
age: 24,
visits: 100,
status: 'In Relationship',
progress: 50,
},
{
firstName: 'tandy',
lastName: 'miller',
age: 40,
visits: 40,
status: 'Single',
progress: 80,
},
{
firstName: 'joe',
lastName: 'dirte',
age: 45,
visits: 20,
status: 'Complicated',
progress: 10,
},
]
// 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
const _features = tableFeatures({}) // util method to create sharable TFeatures object/type
// 4. Define the columns for your table. This uses the new `ColumnDef` type to define columns. Alternatively, check out the createTableHelper/createColumnHelper util for an even more type-safe way to define columns.
const defaultColumns: Array<ColumnDef<typeof _features, Person>> = [
{
accessorKey: 'firstName',
cell: (info) => info.getValue(),
footer: (info) => info.column.id,
},
{
accessorFn: (row) => row.lastName,
id: 'lastName',
cell: (info) => <i>{info.getValue<string>()}</i>,
header: () => <span>Last Name</span>,
footer: (info) => info.column.id,
},
{
accessorKey: 'age',
header: () => 'Age',
footer: (info) => info.column.id,
},
{
accessorKey: 'visits',
header: () => <span>Visits</span>,
footer: (info) => info.column.id,
},
{
accessorKey: 'status',
header: 'Status',
footer: (info) => info.column.id,
},
{
accessorKey: 'progress',
header: 'Profile Progress',
footer: (info) => info.column.id,
},
]
function App() {
// 5. Store data with a stable reference
const [data, setData] = createSignal(defaultData)
const rerender = () => setData(defaultData)
// 6. Create the table instance with required _features, columns, and data
const table = createTable({
_features, // new required option in V9. Tell the table which features you are importing and using (better tree-shaking)
_rowModels: {}, // `Core` row model is now included by default, but you can still override it here
// add additional table options here
columns: defaultColumns,
get data() {
return data()
},
})
return (
<div class="p-2">
<table>
<thead>
<For each={table.getHeaderGroups()}>
{(headerGroup) => (
<tr>
<For each={headerGroup.headers}>
{(header) => (
<th>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext(),
)}
</th>
)}
</For>
</tr>
)}
</For>
</thead>
<tbody>
<For each={table.getRowModel().rows}>
{(row) => (
<tr>
<For each={row.getAllCells()}>
{(cell) => (
<td>
{flexRender(
cell.column.columnDef.cell,
cell.getContext(),
)}
</td>
)}
</For>
</tr>
)}
</For>
</tbody>
<tfoot>
<For each={table.getFooterGroups()}>
{(footerGroup) => (
<tr>
<For each={footerGroup.headers}>
{(header) => (
<th>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.footer,
header.getContext(),
)}
</th>
)}
</For>
</tr>
)}
</For>
</tfoot>
</table>
<div class="h-4" />
<button onClick={() => rerender()} class="border p-2">
Rerender
</button>
</div>
)
}
export default App