-
Notifications
You must be signed in to change notification settings - Fork 28
/
paginator.go
168 lines (153 loc) · 5.47 KB
/
paginator.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package util
import (
"errors"
)
// 获取分页编号序列, 页码从 0 开始, 序列中非负整数表示页码, -1 表示省略,
// 如 [0,1,-1,8,9,10,11,12,-1,15,16] 表示 0,1,...,8,9,10,11,12,...,15,16
// pageNum: 页面数量, 大于 0 的整数
// currentPageIndex: 当前页码, 从 0 开始编码
func Paginator0(pageNum, currentPageIndex int) ([]int, error) {
const (
// 0,1,...,4,5,[6],7,8,...,10,11
paginatorBeginNum = 2 // 分页开头的页码数量
paginatorEndNum = 2 // 分页结尾的页码数量
currentPageIndexFrontNum = 2 // 当前页面前面的页码数量
currentPageIndexBehindNum = 2 // 当前页面后面的页码数量
currentPageIndexRangeNum = currentPageIndexFrontNum + 1 + currentPageIndexBehindNum // 当前页面游标滑块的页码数量
)
if pageNum < 1 {
return nil, errors.New("pageNum < 1")
}
if currentPageIndex < 0 || currentPageIndex >= pageNum {
return nil, errors.New("currentPageIndex out of range")
}
switch {
case pageNum == 1:
return []int{0}, nil
case pageNum <= currentPageIndexRangeNum: // 游标滑块大小内, 肯定是不需要加省略号的
arr := make([]int, pageNum)
for i := range arr {
arr[i] = i
}
return arr, nil
default: // pageNum > currentPageIndexRangeNum
maxPageIndex := pageNum - 1
// 确定当前页面这个游标滑块前后的页码, 如 0,1,...,4,5,[6],7,8,...,10,11 里面的 4 和 8
rangeBeginPageIndex := currentPageIndex - currentPageIndexFrontNum
rangeEndPageIndex := currentPageIndex + currentPageIndexBehindNum
switch {
case rangeBeginPageIndex < 0:
rangeBeginPageIndex = 0
rangeEndPageIndex = currentPageIndexRangeNum - 1 // rangeEndPageIndex == currentPageIndexRangeNum - 1 < pageNum -1 == maxPageIndex
case rangeEndPageIndex > maxPageIndex:
rangeEndPageIndex = maxPageIndex
rangeBeginPageIndex = maxPageIndex - (currentPageIndexRangeNum - 1) // rangeBeginPageIndex == maxPageIndex - (currentPageIndexRangeNum - 1) == pageNum -1 - (currentPageIndexRangeNum - 1) == pageNum - currentPageIndexRangeNum > 0
}
if rangeBeginPageIndex <= paginatorBeginNum { // 跟前面相连
if rangeEndPageIndex >= maxPageIndex-paginatorEndNum { // 跟后面相连
arr := make([]int, pageNum)
for i := range arr {
arr[i] = i
}
return arr, nil
} else { //跟后面不连
arr := make([]int, 0, rangeEndPageIndex+1+1+paginatorEndNum)
for i := 0; i <= rangeEndPageIndex; i++ {
arr = append(arr, i)
}
arr = append(arr, -1)
for i := pageNum - paginatorEndNum; i < pageNum; i++ {
arr = append(arr, i)
}
return arr, nil
}
} else { // 跟前面不连
if rangeEndPageIndex >= maxPageIndex-paginatorEndNum { // 跟后面相连
arr := make([]int, 0, paginatorBeginNum+1+(pageNum-rangeBeginPageIndex))
for i := 0; i < paginatorBeginNum; i++ {
arr = append(arr, i)
}
arr = append(arr, -1)
for i := rangeBeginPageIndex; i < pageNum; i++ {
arr = append(arr, i)
}
return arr, nil
} else { //跟后面不连
arr := make([]int, 0, paginatorBeginNum+1+currentPageIndexRangeNum+1+paginatorEndNum)
for i := 0; i < paginatorBeginNum; i++ {
arr = append(arr, i)
}
arr = append(arr, -1)
for i := rangeBeginPageIndex; i <= rangeEndPageIndex; i++ {
arr = append(arr, i)
}
arr = append(arr, -1)
for i := pageNum - paginatorEndNum; i < pageNum; i++ {
arr = append(arr, i)
}
return arr, nil
}
}
}
}
// 获取分页编号序列, 页码从 1 开始, 序列中正整数表示页码, -1 表示省略,
// 如 [1,2,-1,8,9,10,11,12,-1,15,16] 表示 1,2,...,8,9,10,11,12,...,15,16
// pageNum: 页面数量, 大于 0 的整数
// currentPageIndex: 当前页码, 从 1 开始编码
func Paginator1(pageNum, currentPageIndex int) (arr []int, err error) {
currentPageIndex--
arr, err = Paginator0(pageNum, currentPageIndex)
if err != nil {
return
}
for i := 0; i < len(arr); i++ {
if arr[i] != -1 {
arr[i]++
}
}
return
}
// 获取分页编号序列, 页码从 0 开始, 序列中非负整数表示页码, -1 表示省略,
// 如 [0,1,-1,8,9,10,11,12,-1,15,16] 表示 0,1,...,8,9,10,11,12,...,15,16
// totalItemNum: 总的记录数量, 不是页面数量, 非负整数
// pageSize: 每页显示的数量, 大于 0 的整数
// currentPageIndex: 当前页码, 从 0 开始编码
func Paginator0Ex(totalItemNum, pageSize, currentPageIndex int) (arr []int, pageNum int, err error) {
if totalItemNum < 0 {
err = errors.New("totalItemNum < 0")
return
}
if pageSize <= 0 {
err = errors.New("pageSize <= 0")
return
}
if totalItemNum == 0 {
pageNum = 1
} else {
pageNum = (totalItemNum + pageSize - 1) / pageSize
}
arr, err = Paginator0(pageNum, currentPageIndex)
return
}
// 获取分页编号序列, 页码从 1 开始, 序列中正整数表示页码, -1 表示省略,
// 如 [1,2,-1,8,9,10,11,12,-1,15,16] 表示 1,2,...,8,9,10,11,12,...,15,16
// totalItemNum: 总的记录数量, 不是页面数量, 非负整数
// pageSize: 每页显示的数量, 大于 0 的整数
// currentPageIndex: 当前页码, 从 1 开始编码
func Paginator1Ex(totalItemNum, pageSize, currentPageIndex int) (arr []int, pageNum int, err error) {
if totalItemNum < 0 {
err = errors.New("totalItemNum < 0")
return
}
if pageSize <= 0 {
err = errors.New("pageSize <= 0")
return
}
if totalItemNum == 0 {
pageNum = 1
} else {
pageNum = (totalItemNum + pageSize - 1) / pageSize
}
arr, err = Paginator1(pageNum, currentPageIndex)
return
}