import {
createColumnHelper,
createPaginatedRowModel,
createSortedRowModel,
createTable,
FlexRender,
rowPaginationFeature,
rowSortingFeature,
sortFns,
tableFeatures,
} from '@tanstack/solid-table'
import { For, createSignal } from 'solid-js'
import { makeData } from './makeData'
import type { PaginationState, SortingState } from '@tanstack/solid-table'
import type { Person } from './makeData'
// This example demonstrates managing table state externally via Solid's createSignal instead of letting the table manage its own state internally.
const _features = tableFeatures({
rowPaginationFeature,
rowSortingFeature,
})
const columnHelper = createColumnHelper<typeof _features, Person>()
const columns = columnHelper.columns([
columnHelper.accessor('firstName', {
header: 'First Name',
cell: (info) => info.getValue(),
}),
columnHelper.accessor('lastName', {
header: 'Last Name',
cell: (info) => info.getValue(),
}),
columnHelper.accessor('age', {
header: 'Age',
}),
columnHelper.accessor('visits', {
header: 'Visits',
}),
columnHelper.accessor('status', {
header: 'Status',
}),
columnHelper.accessor('progress', {
header: 'Profile Progress',
}),
])
function App() {
const [data] = createSignal(makeData(1000))
// Manage sorting state with createSignal
const [sorting, setSorting] = createSignal<SortingState>([])
// Manage pagination state with createSignal
const [pagination, setPagination] = createSignal<PaginationState>({
pageIndex: 0,
pageSize: 10,
})
// Create the table and pass state + onChange handlers
const table = createTable({
_features,
_rowModels: {
sortedRowModel: createSortedRowModel(sortFns),
paginatedRowModel: createPaginatedRowModel(),
},
columns,
get data() {
return data()
},
state: {
get sorting() {
return sorting() // connect our sorting state back down to the table
},
get pagination() {
return pagination() // connect our pagination state back down to the table
},
},
onSortingChange: setSorting, // raise sorting state changes to our own state management
onPaginationChange: setPagination, // raise pagination state changes to our own state management
})
return (
<div class="p-2">
<table>
<thead>
<For each={table.getHeaderGroups()}>
{(headerGroup) => (
<tr>
<For each={headerGroup.headers}>
{(header) => (
<th colSpan={header.colSpan}>
{header.isPlaceholder ? null : (
<div
class={
header.column.getCanSort()
? 'cursor-pointer select-none'
: ''
}
onClick={header.column.getToggleSortingHandler()}
>
<FlexRender header={header} />
{{
asc: ' 🔼',
desc: ' 🔽',
}[header.column.getIsSorted() as string] ?? null}
</div>
)}
</th>
)}
</For>
</tr>
)}
</For>
</thead>
<tbody>
<For each={table.getRowModel().rows}>
{(row) => (
<tr>
<For each={row.getAllCells()}>
{(cell) => (
<td>
<FlexRender cell={cell} />
</td>
)}
</For>
</tr>
)}
</For>
</tbody>
</table>
<div class="h-2" />
<div class="flex items-center gap-2">
<button
class="border rounded p-1"
onClick={() => table.setPageIndex(0)}
disabled={!table.getCanPreviousPage()}
>
{'<<'}
</button>
<button
class="border rounded p-1"
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
>
{'<'}
</button>
<button
class="border rounded p-1"
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
>
{'>'}
</button>
<button
class="border rounded p-1"
onClick={() => table.setPageIndex(table.getPageCount() - 1)}
disabled={!table.getCanNextPage()}
>
{'>>'}
</button>
<span class="flex items-center gap-1">
<div>Page</div>
<strong>
{pagination().pageIndex + 1} of {table.getPageCount()}
</strong>
</span>
<span class="flex items-center gap-1">
| Go to page:
<input
type="number"
min="1"
max={table.getPageCount()}
value={pagination().pageIndex + 1}
onInput={(e) => {
const page = e.currentTarget.value
? Number(e.currentTarget.value) - 1
: 0
table.setPageIndex(page)
}}
class="border p-1 rounded w-16"
/>
</span>
<select
value={pagination().pageSize}
onChange={(e) => {
table.setPageSize(Number(e.currentTarget.value))
}}
>
{[10, 20, 30, 40, 50].map((pageSize) => (
<option value={pageSize}>Show {pageSize}</option>
))}
</select>
</div>
<div class="h-4" />
<pre>
{JSON.stringify(
{ sorting: sorting(), pagination: pagination() },
null,
2,
)}
</pre>
</div>
)
}
export default App
import {
createColumnHelper,
createPaginatedRowModel,
createSortedRowModel,
createTable,
FlexRender,
rowPaginationFeature,
rowSortingFeature,
sortFns,
tableFeatures,
} from '@tanstack/solid-table'
import { For, createSignal } from 'solid-js'
import { makeData } from './makeData'
import type { PaginationState, SortingState } from '@tanstack/solid-table'
import type { Person } from './makeData'
// This example demonstrates managing table state externally via Solid's createSignal instead of letting the table manage its own state internally.
const _features = tableFeatures({
rowPaginationFeature,
rowSortingFeature,
})
const columnHelper = createColumnHelper<typeof _features, Person>()
const columns = columnHelper.columns([
columnHelper.accessor('firstName', {
header: 'First Name',
cell: (info) => info.getValue(),
}),
columnHelper.accessor('lastName', {
header: 'Last Name',
cell: (info) => info.getValue(),
}),
columnHelper.accessor('age', {
header: 'Age',
}),
columnHelper.accessor('visits', {
header: 'Visits',
}),
columnHelper.accessor('status', {
header: 'Status',
}),
columnHelper.accessor('progress', {
header: 'Profile Progress',
}),
])
function App() {
const [data] = createSignal(makeData(1000))
// Manage sorting state with createSignal
const [sorting, setSorting] = createSignal<SortingState>([])
// Manage pagination state with createSignal
const [pagination, setPagination] = createSignal<PaginationState>({
pageIndex: 0,
pageSize: 10,
})
// Create the table and pass state + onChange handlers
const table = createTable({
_features,
_rowModels: {
sortedRowModel: createSortedRowModel(sortFns),
paginatedRowModel: createPaginatedRowModel(),
},
columns,
get data() {
return data()
},
state: {
get sorting() {
return sorting() // connect our sorting state back down to the table
},
get pagination() {
return pagination() // connect our pagination state back down to the table
},
},
onSortingChange: setSorting, // raise sorting state changes to our own state management
onPaginationChange: setPagination, // raise pagination state changes to our own state management
})
return (
<div class="p-2">
<table>
<thead>
<For each={table.getHeaderGroups()}>
{(headerGroup) => (
<tr>
<For each={headerGroup.headers}>
{(header) => (
<th colSpan={header.colSpan}>
{header.isPlaceholder ? null : (
<div
class={
header.column.getCanSort()
? 'cursor-pointer select-none'
: ''
}
onClick={header.column.getToggleSortingHandler()}
>
<FlexRender header={header} />
{{
asc: ' 🔼',
desc: ' 🔽',
}[header.column.getIsSorted() as string] ?? null}
</div>
)}
</th>
)}
</For>
</tr>
)}
</For>
</thead>
<tbody>
<For each={table.getRowModel().rows}>
{(row) => (
<tr>
<For each={row.getAllCells()}>
{(cell) => (
<td>
<FlexRender cell={cell} />
</td>
)}
</For>
</tr>
)}
</For>
</tbody>
</table>
<div class="h-2" />
<div class="flex items-center gap-2">
<button
class="border rounded p-1"
onClick={() => table.setPageIndex(0)}
disabled={!table.getCanPreviousPage()}
>
{'<<'}
</button>
<button
class="border rounded p-1"
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
>
{'<'}
</button>
<button
class="border rounded p-1"
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
>
{'>'}
</button>
<button
class="border rounded p-1"
onClick={() => table.setPageIndex(table.getPageCount() - 1)}
disabled={!table.getCanNextPage()}
>
{'>>'}
</button>
<span class="flex items-center gap-1">
<div>Page</div>
<strong>
{pagination().pageIndex + 1} of {table.getPageCount()}
</strong>
</span>
<span class="flex items-center gap-1">
| Go to page:
<input
type="number"
min="1"
max={table.getPageCount()}
value={pagination().pageIndex + 1}
onInput={(e) => {
const page = e.currentTarget.value
? Number(e.currentTarget.value) - 1
: 0
table.setPageIndex(page)
}}
class="border p-1 rounded w-16"
/>
</span>
<select
value={pagination().pageSize}
onChange={(e) => {
table.setPageSize(Number(e.currentTarget.value))
}}
>
{[10, 20, 30, 40, 50].map((pageSize) => (
<option value={pageSize}>Show {pageSize}</option>
))}
</select>
</div>
<div class="h-4" />
<pre>
{JSON.stringify(
{ sorting: sorting(), pagination: pagination() },
null,
2,
)}
</pre>
</div>
)
}
export default App