分页代码:
package pagination
import (
"math"
"strconv"
"strings"
)
type Pagination struct {
total int
page int
limit int
num_links int
url string
text string
textFirst ,textLast, textNext, textPrev string
styleLinks, styleResults string
}
func (p *Pagination) Setup(){
p.limit = 20;
p.num_links = 10;
p.text = "显示 {start} 至 {end} 共 {total} 记录 ({pages} 页)";
p.textFirst = "首页"
p.textLast = "末页"
p.textPrev = "上一页"
p.textNext = "下一页"
p.styleLinks = "links"
p.styleResults = "results"
}
func (p *Pagination) SetUrl(url string){
p.url = url
}
func (p *Pagination) SetTotal(total int){
p.total = total
}
func (p *Pagination) SetPage(page int){
p.page = page
}
func (p *Pagination) SetLimit(limit int){
p.limit = limit
}
func (p *Pagination) SetNumLinks(num_links int){
p.num_links = num_links
}
func (p *Pagination) Render() string {
total := p.total
var page int
if p.page < 1 {
page = 1
} else {
page = p.page
}
var limit int
if p.limit <= 0 {
limit = 10
} else {
limit = p.limit
}
num_links := p.num_links
num_pages := int(math.Ceil(float64(total) / float64(limit)))
var output string
if page > 1 {
output += "<a href=\"" + strings.Replace(p.url, "{page}", "1", -1) + p.textFirst + "</a> <a href=\"" + strings.Replace(p.url, "{page}", strconv.Itoa(p.page - 1), -1) + "\">" + p.textPrev + "</a>";
}
var start,end int
if num_pages > 1 {
if num_pages <= num_links {
start = 1
end = num_pages
} else {
start = page - int(math.Floor(float64(num_links)/float64(2)))
end = page + int(math.Floor(float64(num_links)/float64(2)))
if start < 1 {
end = int(math.Abs(float64(start))) + 1
start = 1
}
if end > num_pages {
start -= end - num_pages
end = num_pages
}
}
if start > 1 {
output += " .... "
}
for i:=start; i <= end ; i++ {
if page == i {
output += " <b>" + strconv.Itoa(i) + "</b>"
} else {
output += " <a href=\"" + strings.Replace(p.url, "{page}", strconv.Itoa(i), -1) + "\">" + strconv.Itoa(i) + "</a>"
}
}
if end < num_pages {
output += " .... "
}
}
var text_start,text_end string
text := p.text
if page < num_pages {
output += " <a href=\"" + strings.Replace(p.url, "{page}", strconv.Itoa(page + 1), -1) + "\">" + p.textNext + "</a> <a href=\"" + strings.Replace(p.url, "{page}", strconv.Itoa(num_pages), -1) + "\">" + p.textLast + "</a>"
}
if total > 0 {
text_start = strconv.Itoa((page -1) * limit + 1)
} else {
text_start = "0"
}
if ((page - 1) * limit) > (total - limit) {
text_end = strconv.Itoa(total)
} else {
text_end = strconv.Itoa(((page - 1) * limit) + limit)
}
text = strings.Replace(text, "{start}", text_start, -1)
text = strings.Replace(text, "{end}", text_end, -1)
text = strings.Replace(text, "{total}", strconv.Itoa(total) , -1)
text = strings.Replace(text, "{pages}", strconv.Itoa(num_pages), -1)
if len(p.styleResults) > 0 {
if len(output) > 0 {
output = "<div class=\"" + p.styleLinks + "\">" + output + "</div>"
} else {
output = ""
}
output += "<div class=\"" + p.styleResults + "\">" + text + "</div>"
} else {
if len(output) > 0 {
output = "<div class=\"" + p.styleLinks + "\">" + output + "</div>"
} else {
output = ""
}
}
return output
}
使用方法
package main
import (
"fmt"
"github.com/proaholic/tools/pagination"
)
func main(){
var url = "http://test.loc/index/index?page={page}"
var page pagination.Pagination
page.Setup()
page.SetUrl(url)
page.SetTotal(5200)
page.SetPage(18)
page.SetNumLinks(20)
fmt.Println(page.Render())
}
输出:
显示 341 至 360 共 5200 记录 (260 页)