上代码
package paging
/**
* 分页功能
* 支飞亚
* 2014-9-1
*/
import (
"fmt"
"strings"
)
const (
RecPageNumber = "{{RecPageNumber}}"
RecLabelHref = "{{RecLabelHref}}"
RecJoinStr = "{{RecJoinStr}}"
RecDisabledAttr = "{{RecDisabledAttr}}"
)
type PageOptions struct {
EnableFirstLastLink bool //是否启用首尾连接 默认false 建议开启
EnablePreNexLink bool //是否启用上一页,下一页连接 默认false 建议开启
Total int // 总条数
TotalPages int // 总页数
PageHtml string // 分页html
LinkItemCount int //生成A标签的个数 默认10个
CurrentPage int //当前页 ,默认1 每次分页,必须在前台设置新的页数,不设置始终默认1
PageSize int //页面大小,默认20
ParamName string //参数名称 默认是page
Href string //A标签的链接地址 参数自行拼接 例如 /list?key=关键词&a=a...paging.RecJoinStr
RecJoinStr string //翻页参数 自行拼接 例如 &page=paging.RecPageNumber
Label string // 页码按钮标签 例如 <a class="button" href="paging.RecLabelHref" ... >paging.RecPageNumber</a>
CurrLabel string // 页码按钮标签 例如 <a class="button current"> paging.LabelAttr href="paging.RecLabelHref" ... >paging.RecPageNumber</a>
HeaderHtml string // 头部html 例如 <div> 页码 </div> 中的 <div>
FooterHtml string // 头部html 例如 <div> 页码 </div> 中的 </div>
DisabledAttr string // "disabled="disabled" style='pointer-events:none;'"
PreviousPageLabel string // 上一页标签 <a class="button current" paging.RecDisabledAttr > paging.LabelAttr href="paging.RecLabelHref" >上一页</a>
NextPageLabel string // 下一页标签 <a class="button current" paging.RecDisabledAttr > paging.LabelAttr href="paging.RecLabelHref" >下一页</a>
FirstPageLabel string // 首页标签 <a class="button current" paging.RecDisabledAttr > paging.LabelAttr href="paging.RecLabelHref" >首页</a>
LastPageLabel string // 尾页标签 <a class="button current" paging.RecDisabledAttr > paging.LabelAttr href="paging.RecLabelHref" >尾页</a>
}
// 计算总页数 pageSize 页面大小 total 总条数
func (po *PageOptions) getTotalPages() {
if po.PageSize == 0 {
po.PageSize = 20
}
if po.Total <= po.PageSize {
po.TotalPages = 1
} else if po.Total > po.PageSize {
temp := po.Total / po.PageSize
if (po.Total % po.PageSize) != 0 {
temp = temp + 1
}
po.TotalPages = temp
}
}
/* 设置分页
示例
po := paging.PageOptions{}
po.EnablePreNexLink = true
po.EnableFirstLastLink = true
po.LinkItemCount = 20
po.CurrentPage = 7
po.PageSize = 10
po.Total = 100
po.Href = "/list?key=关键词&a=a" + paging.RecJoinStr
po.RecJoinStr = "&page=" + paging.RecPageNumber
po.Label = `<a class="button" href="` + paging.RecLabelHref + `">` + paging.RecPageNumber + `</a>`
po.CurrLabel = `<a class="button current" href="` + paging.RecLabelHref + `">` + paging.RecPageNumber + `</a>`
po.HeaderHtml = "<div>"
po.FooterHtml = "</div>"
po.DisabledAttr = "disabled=\"disabled\" style='pointer-events:none;'"
po.PreviousPageLabel = `<a ` + paging.RecDisabledAttr + ` href="` + paging.RecLabelHref + `" >上一页</a>`
po.NextPageLabel = `<a ` + paging.RecDisabledAttr + ` href="` + paging.RecLabelHref + `" >下一页</a>`
po.FirstPageLabel = `<a ` + paging.RecDisabledAttr + ` href="` + paging.RecLabelHref + `" >首页</a>`
po.LastPageLabel = `<a ` + paging.RecDisabledAttr + ` href="` + paging.RecLabelHref + `" >尾页</a>`
po.GetPagerLinks()
_fmt.println(po.PageHtml)
*/
func (po *PageOptions) GetPagerLinks() {
po.Href = strings.Replace(po.Href, RecJoinStr, "%v", -1)
po.RecJoinStr = strings.Replace(po.RecJoinStr, RecPageNumber, "%v", -1)
po.Label = strings.Replace(po.Label, RecLabelHref, "%v", -1)
po.Label = strings.Replace(po.Label, RecPageNumber, "%v", -1)
po.CurrLabel = strings.Replace(po.CurrLabel, RecLabelHref, "%v", -1)
po.CurrLabel = strings.Replace(po.CurrLabel, RecPageNumber, "%v", -1)
po.PreviousPageLabel = strings.Replace(po.PreviousPageLabel, RecDisabledAttr, "%v", -1)
po.PreviousPageLabel = strings.Replace(po.PreviousPageLabel, RecLabelHref, "%v", -1)
po.NextPageLabel = strings.Replace(po.NextPageLabel, RecDisabledAttr, "%v", -1)
po.NextPageLabel = strings.Replace(po.NextPageLabel, RecLabelHref, "%v", -1)
po.FirstPageLabel = strings.Replace(po.FirstPageLabel, RecDisabledAttr, "%v", -1)
po.FirstPageLabel = strings.Replace(po.FirstPageLabel, RecLabelHref, "%v", -1)
po.LastPageLabel = strings.Replace(po.LastPageLabel, RecDisabledAttr, "%v", -1)
po.LastPageLabel = strings.Replace(po.LastPageLabel, RecLabelHref, "%v", -1)
po.getTotalPages()
po.SetDefault()
if po.TotalPages <= po.LinkItemCount {
po.PageHtml = po.type1() //显示完全 12345678910
} else if po.TotalPages > po.LinkItemCount {
if po.CurrentPage < po.LinkItemCount {
po.PageHtml = po.type2() //123456789...200
} else {
if po.CurrentPage+po.LinkItemCount < po.TotalPages {
po.PageHtml = po.type3()
} else {
po.PageHtml = po.type4()
}
}
}
}
/**
* 设置默认值
*/
func (po *PageOptions) SetDefault() *PageOptions {
if po.CurrentPage >= po.TotalPages {
po.CurrentPage = po.TotalPages
}
if po.CurrentPage <= 1 {
po.CurrentPage = 1
}
if po.LinkItemCount == 0 {
po.LinkItemCount = 10
}
if po.PageSize == 0 {
po.PageSize = 20
}
if len(po.ParamName) <= 0 {
po.ParamName = "page"
}
return po
}
/**
* 1...197 198 199 200
*/
func (po *PageOptions) type4() string {
var rs string = ""
rs += po.getHeader()
RecJoin := fmt.Sprintf(po.RecJoinStr, 1)
Href := fmt.Sprintf(po.Href, RecJoin)
Label := fmt.Sprintf(po.Label, Href, 1)
rs += Label
Label = fmt.Sprintf(po.Label, "", "...")
rs += Label
for i := po.TotalPages - po.LinkItemCount; i <= po.TotalPages; i++ {
if po.CurrentPage != i {
RecJoin = fmt.Sprintf(po.RecJoinStr, i)
Href = fmt.Sprintf(po.Href, RecJoin)
Label = fmt.Sprintf(po.Label, Href, i)
rs += Label
} else {
RecJoin = fmt.Sprintf(po.RecJoinStr, i)
Href = fmt.Sprintf(po.Href, RecJoin)
Label = fmt.Sprintf(po.CurrLabel, Href, i)
rs += Label
}
}
rs += po.getFooter()
return rs
}
/**
* 1...6 7 8 9 10 11 12 13 14 15... 200
*/
func (po *PageOptions) type3() string {
var rs string = ""
rs += po.getHeader()
RecJoin := fmt.Sprintf(po.RecJoinStr, 1)
Href := fmt.Sprintf(po.Href, RecJoin)
Label := fmt.Sprintf(po.Label, Href, 1)
rs += Label
Label = fmt.Sprintf(po.Label, "", "...")
rs += Label
for i := po.CurrentPage - po.LinkItemCount/2 + 1; i <= po.CurrentPage+po.LinkItemCount/2-1; i++ {
if po.CurrentPage != i {
RecJoin = fmt.Sprintf(po.RecJoinStr, i)
Href = fmt.Sprintf(po.Href, RecJoin)
Label = fmt.Sprintf(po.Label, Href, i)
rs += Label
} else {
RecJoin = fmt.Sprintf(po.RecJoinStr, i)
Href = fmt.Sprintf(po.Href, RecJoin)
Label = fmt.Sprintf(po.CurrLabel, Href, i)
rs += Label
}
}
Label = fmt.Sprintf(po.Label, "", "...")
rs += Label
RecJoin = fmt.Sprintf(po.RecJoinStr, po.TotalPages)
Href = fmt.Sprintf(po.Href, RecJoin)
Label = fmt.Sprintf(po.Label, Href, po.TotalPages)
rs += Label
rs += po.getFooter()
return rs
}
/**
* po.TotalPages > po.LinkItemCount po.CurrentPage < po.LinkItemCount
* 123456789...200
*/
func (po *PageOptions) type2() string {
var rs string = ""
rs += po.getHeader()
for i := 1; i <= po.LinkItemCount+1; i++ {
if i == po.LinkItemCount {
RecJoin := fmt.Sprintf(po.RecJoinStr, i)
Href := fmt.Sprintf(po.Href, RecJoin)
Label := fmt.Sprintf(po.Label, Href, "...")
rs += Label
} else if i == po.LinkItemCount+1 {
RecJoin := fmt.Sprintf(po.RecJoinStr, po.TotalPages)
Href := fmt.Sprintf(po.Href, RecJoin)
Label := fmt.Sprintf(po.Label, Href, po.TotalPages)
rs += Label
} else {
if po.CurrentPage != i {
RecJoin := fmt.Sprintf(po.RecJoinStr, i)
Href := fmt.Sprintf(po.Href, RecJoin)
Label := fmt.Sprintf(po.Label, Href, i)
rs += Label
} else {
RecJoin := fmt.Sprintf(po.RecJoinStr, i)
Href := fmt.Sprintf(po.Href, RecJoin)
Label := fmt.Sprintf(po.CurrLabel, Href, i)
rs += Label
}
}
}
rs += po.getFooter()
return rs
}
/**
* po.TotalPages <= po.LinkItemCount
* 显示完全 12345678910
*/
func (po *PageOptions) type1() string {
var rs string = ""
rs += po.getHeader()
for i := 1; i <= po.TotalPages; i++ {
if po.CurrentPage != i {
RecJoin := fmt.Sprintf(po.RecJoinStr, i)
Href := fmt.Sprintf(po.Href, RecJoin)
Label := fmt.Sprintf(po.Label, Href, i)
rs += Label
} else {
RecJoin := fmt.Sprintf(po.RecJoinStr, i)
Href := fmt.Sprintf(po.Href, RecJoin)
Label := fmt.Sprintf(po.CurrLabel, Href, i)
rs += Label
}
}
rs += po.getFooter()
return rs
}
/**
* 头部
*/
func (po *PageOptions) getHeader() string {
var rs string = po.HeaderHtml
if po.EnableFirstLastLink { //当首页,尾页都设定的时候,就显示
RecJoin := fmt.Sprintf(po.RecJoinStr, 1)
Href := fmt.Sprintf(po.Href, RecJoin)
Label := fmt.Sprintf(po.FirstPageLabel, po.judgeDisable(false), Href)
rs += Label
}
if po.EnablePreNexLink {
var a int = po.CurrentPage - 1
if po.CurrentPage == 1 {
a = 1
}
// 上一页
RecJoin := fmt.Sprintf(po.RecJoinStr, a)
Href := fmt.Sprintf(po.Href, RecJoin)
Label := fmt.Sprintf(po.PreviousPageLabel, po.judgeDisable(false), Href)
rs += Label
}
return rs
}
/**
* 尾部
*/
func (po *PageOptions) getFooter() string {
var rs string = ""
if po.EnablePreNexLink {
var a int = po.CurrentPage + 1
if po.CurrentPage == po.TotalPages {
a = po.TotalPages
}
RecJoin := fmt.Sprintf(po.RecJoinStr, a)
Href := fmt.Sprintf(po.Href, RecJoin)
Label := fmt.Sprintf(po.NextPageLabel, po.judgeDisable(true), Href)
rs += Label
}
if po.EnableFirstLastLink { //当首页,尾页都设定的时候,就显示
RecJoin := fmt.Sprintf(po.RecJoinStr, po.TotalPages)
Href := fmt.Sprintf(po.Href, RecJoin)
Label := fmt.Sprintf(po.LastPageLabel, po.judgeDisable(true), Href)
rs += Label
}
rs += po.FooterHtml
return rs
}
//判断首页尾页 上一页下一页是否能用
func (po *PageOptions) judgeDisable(isFooter bool) string {
var rs string = ""
//判断头部
if !isFooter {
if po.CurrentPage == 1 {
rs = po.DisabledAttr
}
} else {
if po.CurrentPage == po.TotalPages {
rs = po.DisabledAttr
}
}
return rs
}
声明:
本文采用
BY-NC-SA
协议进行授权,如无注明均为原创,转载请注明转自
一颗大萝北
本文地址: goland web beego gin 网页分页
本文地址: goland web beego gin 网页分页