Math.ceil(totalItems / pageSize). For example, 100 items with pageSize 10 = 10 pages. 105 items with pageSize 10 = 11 pages (the last page has 5 items).
How do you calculate total pages in pagination?
More FAQs in Building a Pagination Component: A Complete Guide
Calculate total pages (Math.ceil(total / pageSize)). Slice the data for the current page: data.slice((page - 1) * pageSize, page * pageSize). Render page numbers with ellipsis for many pages. Highlight the current page. Add prev/next buttons with disabled states.
If total pages > 7, show: first few pages, ..., current and neighbors, ..., last few pages. If current is near the start: [1, 2, 3, 4, 5, ..., total]. If near the end: [1, ..., total-4, total-3, total-2, total-1, total]. If in the middle: [1, ..., current-1, current, current+1, ..., total].
Client-side: fetch all data, slice in the browser (data.slice). Good for small datasets. Server-side: fetch only the current page from the API (/api/data?page=1&size=10). Good for large datasets. Server-side is more scalable.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.