-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathpager.go
More file actions
82 lines (67 loc) · 1.68 KB
/
pager.go
File metadata and controls
82 lines (67 loc) · 1.68 KB
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
package main
import (
"fmt"
"strconv"
)
// Pagination Component for html
type PageItem struct {
Label string
Url string
IsCurrent bool
}
func (this *PageItem) Class() string {
if this.IsCurrent {
return "active"
}
return ""
}
type Pager struct {
CurrentPage int
PageSize int
Total int
MaxItem int
UrlPattern string
}
func NewPager(pageSize int, total int) *Pager {
return &Pager{0, pageSize, total, 10, ""}
}
func (this *Pager) Offset() int {
return this.CurrentPage * this.PageSize
}
func (this *Pager) Limit() int {
return this.PageSize
}
func (this *Pager) Page() int {
return (this.Total + this.PageSize - 1) / this.PageSize
}
func (this *Pager) IsVisible() bool {
return this.Total > this.PageSize
}
func (this *Pager) IsFirstVisible() bool {
startPage := MaxInt(0, this.CurrentPage-this.MaxItem/2)
return startPage > 0
}
func (this *Pager) FirstItem() *PageItem {
url := fmt.Sprintf(this.UrlPattern, this.PageSize, 0)
return &PageItem{"Start", url, false}
}
func (this *Pager) IsEndVisible() bool {
startPage := MaxInt(0, this.CurrentPage-this.MaxItem/2)
return startPage+this.MaxItem < this.Page()
}
func (this *Pager) EndItem() *PageItem {
url := fmt.Sprintf(this.UrlPattern, this.PageSize-1, this.Page())
return &PageItem{"End", url, false}
}
func (this *Pager) Pages() []*PageItem {
startPage := MaxInt(0, this.CurrentPage-this.MaxItem/2)
maxPage := this.Page()
pages := []*PageItem{}
for i := startPage; i < startPage+this.MaxItem && i < maxPage; i++ {
isCurrent := i == this.CurrentPage
url := fmt.Sprintf(this.UrlPattern, i)
item := &PageItem{strconv.Itoa(i + 1), url, isCurrent}
pages = append(pages, item)
}
return pages
}