Web / TanStack Interview Questions
How do you integrate TanStack Table with server-side sorting, filtering and pagination?
Set manualSorting and manualPagination to true. TanStack Table manages UI state (current sort, page) but does not process rows — you read that state and send it to your API.
<script setup lang="ts">
import { ref, computed } from "vue"
import { useQuery, keepPreviousData } from "@tanstack/vue-query"
import { useVueTable, createColumnHelper, getCoreRowModel,
type SortingState, type PaginationState } from "@tanstack/vue-table"
const sorting = ref<SortingState>([])
const pagination = ref<PaginationState>({ pageIndex:0, pageSize:20 })
const { data:serverData } = useQuery({
queryKey: computed(()=>["users",{ sort:sorting.value[0], ...pagination.value }]),
queryFn: ()=>fetchUsers({
sortBy: sorting.value[0]?.id,
sortDir: sorting.value[0]?.desc?"desc":"asc",
page: pagination.value.pageIndex,
pageSize: pagination.value.pageSize,
}),
placeholderData: keepPreviousData,
})
const ch=createColumnHelper<User>()
const columns=[ch.accessor("name",{header:"Name",enableSorting:true})]
const table=useVueTable({
get data(){ return serverData.value?.rows??[] },
get rowCount(){ return serverData.value?.total??0 },
columns,
manualSorting: true,
manualPagination: true,
state:{
get sorting(){ return sorting.value },
get pagination(){ return pagination.value },
},
onSortingChange: v=>{ sorting.value=typeof v==="function"?v(sorting.value):v },
onPaginationChange: v=>{ pagination.value=typeof v==="function"?v(pagination.value):v },
getCoreRowModel: getCoreRowModel(),
})
</script>
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
