<script lang="ts">
import type {
ColumnDef,
ColumnVisibilityState,
Updater,
} from '@tanstack/svelte-table'
import {
columnVisibilityFeature,
FlexRender,
createTable,
isFunction,
tableFeatures,
} from '@tanstack/svelte-table'
import './index.css'
type Person = {
firstName: string
lastName: string
age: number
visits: number
status: string
progress: number
}
const defaultData: 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,
},
]
const columns: ColumnDef<typeof _features, Person>[] = [
{
header: 'Name',
footer: (props) => props.column.id,
columns: [
{
accessorKey: 'firstName',
cell: (info) => info.getValue(),
footer: (props) => props.column.id,
},
{
accessorFn: (row) => row.lastName,
id: 'lastName',
cell: (info) => info.getValue(),
header: () => 'Last Name',
footer: (props) => props.column.id,
},
],
},
{
header: 'Info',
footer: (props) => props.column.id,
columns: [
{
accessorKey: 'age',
header: () => 'Age',
footer: (props) => props.column.id,
},
{
header: 'More Info',
columns: [
{
accessorKey: 'visits',
header: () => 'Visits',
footer: (props) => props.column.id,
},
{
accessorKey: 'status',
header: 'Status',
footer: (props) => props.column.id,
},
{
accessorKey: 'progress',
header: 'Profile Progress',
footer: (props) => props.column.id,
},
],
},
],
},
]
const _features = tableFeatures({ columnVisibilityFeature })
let columnVisibility = $state<ColumnVisibilityState>({})
function setColumnVisibility(updater: Updater<ColumnVisibilityState>) {
if (isFunction(updater)) {
columnVisibility = updater(columnVisibility)
} else columnVisibility = updater
}
const table = createTable({
_features,
_rowModels: {},
data: defaultData,
columns,
state: {
get columnVisibility() {
return columnVisibility
},
},
onColumnVisibilityChange: setColumnVisibility,
debugTable: true,
})
</script>
<div class="p-2">
<div class="inline-block border border-black shadow rounded">
<div class="px-1 border-b border-black">
<label>
<input
checked={table.getIsAllColumnsVisible()}
onchange={(e) => {
console.info(table.getToggleAllColumnsVisibilityHandler()(e))
}}
type="checkbox"
/>{' '}
Toggle All
</label>
</div>
{#each table.getAllLeafColumns() as column}
<div class="px-1">
<label>
<input
checked={column.getIsVisible()}
onchange={column.getToggleVisibilityHandler()}
type="checkbox"
/>{' '}
{column.id}
</label>
</div>
{/each}
</div>
<div class="h-4"></div>
<table>
<thead>
{#each table.getHeaderGroups() as headerGroup}
<tr>
{#each headerGroup.headers as header}
<th colSpan={header.colSpan}>
{#if !header.isPlaceholder}
<FlexRender
content={header.column.columnDef.header}
context={header.getContext()}
/>
{/if}
</th>
{/each}
</tr>
{/each}
</thead>
<tbody>
{#each table.getCoreRowModel().rows.slice(0, 20) as row}
<tr>
{#each row.getVisibleCells() as cell}
<td>
<FlexRender
content={cell.column.columnDef.cell}
context={cell.getContext()}
/>
</td>
{/each}
</tr>
{/each}
</tbody>
<tfoot>
{#each table.getFooterGroups() as footerGroup}
<tr>
{#each footerGroup.headers as header}
<th colSpan={header.colSpan}>
{#if !header.isPlaceholder}
<FlexRender
content={header.column.columnDef.header}
context={header.getContext()}
/>
{/if}
</th>
{/each}
</tr>
{/each}
</tfoot>
</table>
<div class="h-4"></div>
<pre>{JSON.stringify(table.store.state.columnVisibility, null, 2)}</pre>
</div>