Web / TanStack Interview Questions
How do you implement pagination with TanStack Table?
Add getPaginationRowModel() to the table options. The table exposes page-navigation helpers and pagination state. Controlled pagination allows URL sync.
<script setup lang="ts"> import { useVueTable, createColumnHelper, getCoreRowModel, getPaginationRowModel, } from "@tanstack/vue-table" import { ref } from "vue" const data=ref(largeDataset) const table=useVueTable({ get data(){ return data.value }, columns, getCoreRowModel: getCoreRowModel(), getPaginationRowModel: getPaginationRowModel(), initialState: { pagination:{ pageSize:20, pageIndex:0 } }, }) </script> <template> <tr v-for="row in table.getRowModel().rows" :key="row.id"> <td v-for="cell in row.getVisibleCells()" :key="cell.id">...</td> </tr> <div> <button @click="table.previousPage()" :disabled="!table.getCanPreviousPage()">Prev</button> <span>Page {{ table.getState().pagination.pageIndex+1 }} of {{ table.getPageCount() }}</span> <button @click="table.nextPage()" :disabled="!table.getCanNextPage()">Next</button> <select :value="table.getState().pagination.pageSize" @change="table.setPageSize(Number(($event.target as HTMLSelectElement).value))"> <option v-for="n in [10,20,50]" :key="n" :value="n">Show {{n}}</option> </select> <; </template>
More Related questions...