-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugins.go
119 lines (117 loc) · 2.87 KB
/
plugins.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
package tplengine
import (
"encoding/json"
"html/template"
"math"
)
// Paging 分页参数
type Paging struct{
// 要显示的页数(page numbers to display)
PageNumbers []int
PageNo int
RowsPerPage int
TotalRows int
TotalPages int
StartPage, EndPage int
PrePageNo, NextPageNo int
}
var plugins = template.FuncMap{
"json": func(d interface{}) template.HTML {
b, err := json.Marshal(d)
if err != nil {
return ""
}
return template.HTML(string(b))
},
"list": func(vals ...interface{}) []interface{} {
return append([]interface{}{}, vals...)
},
"map": func() map[string]interface{} {
return map[string]interface{}{}
},
"addToMap": func(name string, val interface{}, m map[string]interface{}) map[string]interface{} {
m[name] = val
return m
},
"sum": func(args ...int) int {
sum := 0
for _, n := range args {
sum += n
}
return sum
},
"minus": func(args ...int) int {
val := 0
for _, n := range args {
val -= n
}
return val
},
"between": func(val, min, max int) bool {
return val >= min && val <= max
},
// 三元运算 ?:
"ternary": func(comp bool, trueOption, falseOption interface{}) interface{} {
if comp {
return trueOption
}
return falseOption
},
"divide": func(i, j int) int {
return i / j
},
// 生成分页参数对象
"paging": func(displayPageCount, pageNo, rowsPerPage, totalRows int)*Paging{
paging := &Paging{}
totalPages := int(math.Ceil(float64(totalRows) / float64(rowsPerPage)))
if totalPages < 1 {
return paging
}
if pageNo > totalPages {
pageNo = totalPages
}
paging.TotalPages = totalPages
paging.TotalRows = totalRows
paging.RowsPerPage = rowsPerPage
paging.PageNo = pageNo
sidePageCount := displayPageCount/2
var upperCount, lowerCount int
lowerCount, upperCount = sidePageCount, sidePageCount
// 为偶数时总数会大于总长度,因此选择将起始页向后推一页.
if sidePageCount * 2 == displayPageCount{
lowerCount = sidePageCount - 1
}
paging.EndPage = pageNo + upperCount
d := 0
// 如果终止页超过总页数,则将多出的页数补偿到起始页
if paging.EndPage > totalPages {
d = paging.EndPage - totalPages
paging.EndPage = totalPages
}
paging.StartPage = pageNo - lowerCount - d
// 起始页补偿
if paging.StartPage < 1 {
d = 1 - paging.StartPage
paging.StartPage = 1
paging.EndPage += d
if paging.EndPage > paging.TotalPages {
paging.EndPage = paging.TotalPages
}
}
if paging.StartPage > paging.PageNo {
paging.StartPage = paging.PageNo
}
paging.PrePageNo = paging.StartPage - 1
if paging.PrePageNo < 1 {
paging.PrePageNo = 1
}
paging.NextPageNo = paging.EndPage + 1
if paging.NextPageNo > paging.TotalPages {
paging.NextPageNo = paging.TotalPages
}
for i := paging.StartPage; i <= paging.EndPage; i++ {
paging.PageNumbers = append(paging.PageNumbers, i)
}
return paging
},
}