-
Notifications
You must be signed in to change notification settings - Fork 2
/
pagination.go
90 lines (72 loc) · 1.56 KB
/
pagination.go
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package search
import "math"
type Pagination struct {
Total int
CurrentPage int
TotalPages int
PageSize int
}
func newPagination(total, currentPage, pageSize int) *Pagination {
return &Pagination{
Total: total,
CurrentPage: currentPage,
TotalPages: int(math.Ceil(float64(total) / float64(pageSize))),
PageSize: pageSize,
}
}
func (p *Pagination) Start() int {
return (p.CurrentPage-1)*p.PageSize + 1
}
func (p *Pagination) End() int {
end := p.CurrentPage * p.PageSize
if end < p.Total {
return end
}
return p.Total
}
func (p *Pagination) HasPrevious() bool {
return p.CurrentPage > 1
}
func (p *Pagination) Previous() int {
return p.CurrentPage - 1
}
func (p *Pagination) HasNext() bool {
return p.Total > p.CurrentPage*p.PageSize
}
func (p *Pagination) Next() int {
return p.CurrentPage + 1
}
func (p *Pagination) Pages() []int {
if p.TotalPages <= 7 {
pages := make([]int, p.TotalPages)
for i := 0; i < p.TotalPages; i++ {
pages[i] = i + 1
}
return pages
}
pages := make([]int, 0, 7)
if p.CurrentPage > 1 {
prev := p.CurrentPage - 1
switch prev {
case 1:
pages = append(pages, 1)
case 2:
pages = append(pages, 1, 2)
default:
pages = append(pages, 1, -1, prev)
}
}
pages = append(pages, p.CurrentPage)
if p.CurrentPage < p.TotalPages {
next := p.CurrentPage + 1
switch next {
case p.TotalPages:
pages = append(pages, p.TotalPages)
case p.TotalPages - 1:
pages = append(pages, next, p.TotalPages)
default:
pages = append(pages, next, -1, p.TotalPages)
}
}
return pages
}