<script lang="ts">
import { createTable, FlexRender, tableFeatures
} from '@tanstack/svelte-table'
import type { ColumnDef } from '@tanstack/svelte-table'
import { makeData, type Person } from './makeData'
import './index.css'
const features = tableFeatures({
})
const columns: Array<ColumnDef<typeof features, Person>> = [
{
accessorKey: 'firstName',
header: 'First Name',
cell: (info) => info.getValue(),
},
{
accessorFn: (row) => row.lastName,
id: 'lastName',
header: () => 'Last Name',
cell: (info) => info.getValue(),
},
{
accessorFn: (row) => Number(row.age),
id: 'age',
header: () => 'Age',
cell: (info) => info.renderValue(),
},
{
accessorKey: 'visits',
header: () => 'Visits',
},
{
accessorKey: 'status',
header: 'Status',
},
{
accessorKey: 'progress',
header: 'Profile Progress',
},
]
let data = $state(makeData(20))
const refreshData = () => { data = makeData(20) }
const stressTest = () => { data = makeData(1_000) }
const table = createTable({
debugTable: true,
features,
columns,
get data() {
return data
},
})
</script>
<div class="demo-root">
<div>
<button onclick={() => refreshData()
}>Regenerate Data</button>
<button onclick={() => stressTest()}>Stress Test (1k rows)</button>
</div>
<table>
<thead>
{#each table.getHeaderGroups() as headerGroup (headerGroup.id)
}
<tr>
{#each headerGroup.headers as header (header.id)}
<th>
{#if !header.isPlaceholder}
<FlexRender header={header} />
{/if}
</th>
{/each}
</tr>
{/each}
</thead>
<tbody>
{#each table.getRowModel().rows as row (row.id)}
<tr>
{#each row.getAllCells() as cell (cell.id)}
<td>
<FlexRender cell={cell} />
</td>
{/each}
</tr>
{/each}
</tbody>
<tfoot>
{#each table.getFooterGroups() as footerGroup (footerGroup.id)}
<tr>
{#each footerGroup.headers as header (header.id)}
<th>
{#if !header.isPlaceholder}
<FlexRender footer={header} />
{/if}
</th>
{/each}
</tr>
{/each}
</tfoot>
</table>
<div class="spacer-md"></div>
</div>