-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagination.js
50 lines (38 loc) · 1.16 KB
/
pagination.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const totalItems = 103
const limit = 10
const page = 3
const totalPages = Math.ceil(totalItems / limit)
const startPage = 1
const lastPage = totalPages
const buildPagination = (delta) => {
let pageArray = []
// Add the first page
pageArray.push(startPage)
// Add the first `delta` pages in close order
for (let i = 1; i < delta; i++) {
if (page + i < totalPages) {
pageArray.push(page + i)
}
}
// Calculate the middle page between the current page and the last page
const middlePage = Math.floor((page + lastPage) / 2)
// Add ellipsis if necessary before middle page
if (pageArray[pageArray.length - 1] < middlePage - 1) {
pageArray.push("...")
}
// Add the middle page
if (!pageArray.includes(middlePage) && middlePage > page) {
pageArray.push(middlePage)
}
// Add ellipsis if necessary after middle page
if (middlePage < lastPage - 1) {
pageArray.push("...")
}
// Add the last page
if (!pageArray.includes(lastPage)) {
pageArray.push(lastPage)
}
return pageArray
}
const pageArray = buildPagination(3)
console.log(pageArray)