gf/g/util/gpage/gpage.go

304 lines
9.9 KiB
Go
Raw Normal View History

// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
2018-04-22 10:07:24 +08:00
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2018-04-22 10:07:24 +08:00
2019-01-15 23:27:47 +08:00
// Package gpage provides useful paging functionality for web pages.
2018-04-22 10:07:24 +08:00
package gpage
import (
2018-04-22 11:26:19 +08:00
"fmt"
"math"
url2 "net/url"
"github.com/gogf/gf/g/util/gconv"
"github.com/gogf/gf/g/net/ghttp"
"github.com/gogf/gf/g/text/gregex"
"github.com/gogf/gf/g/text/gstr"
2018-08-06 22:38:55 +08:00
"strings"
2018-04-22 10:07:24 +08:00
)
// 分页对象
2018-04-22 10:07:24 +08:00
type Page struct {
2018-07-30 10:28:07 +08:00
Url *url2.URL // 当前页面的URL对象
Router *ghttp.Router // 当前页面的路由对象(与gf框架耦合在静态分页下有效)
UrlTemplate string // URL生成规则内部可使用{.page}变量指定页码
2018-07-30 10:28:07 +08:00
TotalSize int // 总共数据条数
TotalPage int // 总页数
CurrentPage int // 当前页码
PageName string // 分页参数名称(GET参数)
NextPageTag string // 下一页标签
PrevPageTag string // 上一页标签
FirstPageTag string // 首页标签
LastPageTag string // 尾页标签
PrevBar string // 上一分页条
NextBar string // 下一分页条
PageBarNum int // 控制分页条的数量
AjaxActionName string // AJAX方法名当该属性有值时表示使用AJAX分页
2018-04-22 10:07:24 +08:00
}
// 创建一个分页对象,输入参数分别为:
// 总数量、每页数量、当前页码、当前的URL(URI+QUERY)、(可选)路由规则(例如: /user/list/:page、/order/list/*page、/order/list/{page}.html)
2018-07-30 10:28:07 +08:00
func New(TotalSize, perPage int, CurrentPage interface{}, url string, router...*ghttp.Router) *Page {
u, _ := url2.Parse(url)
2018-04-22 11:26:19 +08:00
page := &Page {
PageName : "page",
PrevPageTag : "<",
NextPageTag : ">",
FirstPageTag : "|<",
LastPageTag : ">|",
PrevBar : "<<",
NextBar : ">>",
TotalSize : TotalSize,
2018-05-24 17:16:03 +08:00
TotalPage : int(math.Ceil(float64(TotalSize)/float64(perPage))),
2018-04-23 11:20:48 +08:00
CurrentPage : 1,
PageBarNum : 10,
Url : u,
}
2018-04-23 11:20:48 +08:00
curPage := gconv.Int(CurrentPage)
if curPage > 0 {
page.CurrentPage = curPage
}
2018-07-30 10:28:07 +08:00
if len(router) > 0 {
page.Router = router[0]
2018-04-22 11:26:19 +08:00
}
return page
2018-04-22 10:07:24 +08:00
}
2018-04-22 11:26:19 +08:00
// 启用AJAX分页
func (page *Page) EnableAjax(actionName string) {
page.AjaxActionName = actionName
2018-04-22 10:07:24 +08:00
}
// 设置URL生成规则模板模板中可使用{.page}变量指定页码位置
func (page *Page) SetUrlTemplate(template string) {
page.UrlTemplate = template
}
2018-04-22 11:26:19 +08:00
// 获取显示"下一页"的内容.
func (page *Page) NextPage(styles ... string) string {
var curStyle, style string
if len(styles) > 0 {
curStyle = styles[0]
}
if len(styles) > 1 {
style = styles[0]
}
if page.CurrentPage < page.TotalPage {
return page.GetLink(page.GetUrl(page.CurrentPage + 1), page.NextPageTag, "下一页", style)
2018-04-22 11:26:19 +08:00
}
return fmt.Sprintf(`<span class="%s">%s</span>`, curStyle, page.NextPageTag)
2018-04-22 10:07:24 +08:00
}
2018-04-22 11:26:19 +08:00
/// 获取显示“上一页”的内容
func (page *Page) PrevPage(styles ... string) string {
var curStyle, style string
if len(styles) > 0 {
curStyle = styles[0]
}
if len(styles) > 1 {
style = styles[0]
}
if page.CurrentPage > 1 {
return page.GetLink(page.GetUrl(page.CurrentPage - 1), page.PrevPageTag, "上一页", style)
2018-04-22 11:26:19 +08:00
}
return fmt.Sprintf(`<span class="%s">%s</span>`, curStyle, page.PrevPageTag)
2018-04-22 10:07:24 +08:00
}
/**
* 获取显示首页的代码
*
* @return string
*/
func (page *Page) FirstPage(styles ... string) string {
var curStyle, style string
if len(styles) > 0 {
curStyle = styles[0]
}
if len(styles) > 1 {
style = styles[0]
}
if page.CurrentPage == 1 {
return fmt.Sprintf(`<span class="%s">%s</span>`, curStyle, page.FirstPageTag)
2018-04-22 11:26:19 +08:00
}
return page.GetLink(page.GetUrl(1), page.FirstPageTag, "第一页", style)
2018-04-22 11:26:19 +08:00
}
// 获取显示“尾页”的内容
func (page *Page) LastPage(styles ... string) string {
var curStyle, style string
if len(styles) > 0 {
curStyle = styles[0]
}
if len(styles) > 1 {
style = styles[0]
}
if page.CurrentPage == page.TotalPage {
return fmt.Sprintf(`<span class="%s">%s</span>`, curStyle, page.LastPageTag)
2018-04-22 11:26:19 +08:00
}
return page.GetLink(page.GetUrl(page.TotalPage), page.LastPageTag, "最后页", style)
2018-04-22 11:26:19 +08:00
}
// 获得分页条列表内容
func (page *Page) PageBar(styles ... string) string {
var curStyle, style string
if len(styles) > 0 {
curStyle = styles[0]
}
if len(styles) > 1 {
style = styles[0]
}
plus := int(math.Ceil(float64(page.PageBarNum / 2)))
if page.PageBarNum - plus + page.CurrentPage > page.TotalPage {
plus = page.PageBarNum - page.TotalPage + page.CurrentPage
2018-04-22 11:26:19 +08:00
}
begin := page.CurrentPage - plus + 1
2018-04-22 11:26:19 +08:00
if begin < 1 {
begin = 1
}
ret := ""
for i := begin; i < begin + page.PageBarNum; i++ {
if i <= page.TotalPage {
if i != page.CurrentPage {
ret += page.GetLink(page.GetUrl(i), gconv.String(i), style, "")
2018-04-22 11:26:19 +08:00
} else {
ret += fmt.Sprintf(`<span class="%s">%d</span>`, curStyle, i)
2018-04-22 11:26:19 +08:00
}
} else {
break
}
}
return ret
2018-04-22 10:07:24 +08:00
}
// 获取基于select标签的显示跳转按钮的代码
func (page *Page) SelectBar() string {
ret := `<select name="gpage_select" onchange="window.location.href=this.value">`
for i := 1; i <= page.TotalPage; i++ {
if i == page.CurrentPage {
ret += fmt.Sprintf(`<option value="%s" selected>%d</option>`, page.GetUrl(i), i)
2018-04-22 11:26:19 +08:00
} else {
ret += fmt.Sprintf(`<option value="%s">%d</option>`, page.GetUrl(i), i)
2018-04-22 11:26:19 +08:00
}
}
ret += "</select>"
return ret
2018-04-22 10:07:24 +08:00
}
// 预定义的分页显示风格内容
func (page *Page) GetContent(mode int) string {
switch mode {
case 1:
page.NextPageTag = "下一页"
page.PrevPageTag = "上一页"
return fmt.Sprintf(
`%s <span class="current">%d</span> %s`,
page.PrevPage(),
page.CurrentPage,
page.NextPage(),
)
case 2:
page.NextPageTag = "下一页>>"
page.PrevPageTag = "<<上一页"
page.FirstPageTag = "首页"
page.LastPageTag = "尾页"
return fmt.Sprintf(
`%s%s<span class="current">[第%d页]</span>%s%s第%s页`,
page.FirstPage(),
page.PrevPage(),
page.CurrentPage,
page.NextPage(),
page.LastPage(),
page.SelectBar(),
)
case 3:
page.NextPageTag = "下一页"
page.PrevPageTag = "上一页"
page.FirstPageTag = "首页"
page.LastPageTag = "尾页"
pageStr := page.FirstPage()
pageStr += page.PrevPage()
pageStr += page.PageBar("current")
pageStr += page.NextPage()
pageStr += page.LastPage()
pageStr += fmt.Sprintf(
`<span>当前页%d/%d</span> <span>共%d条</span>`,
page.CurrentPage,
page.TotalPage,
page.TotalSize,
)
return pageStr
case 4:
page.NextPageTag = "下一页"
page.PrevPageTag = "上一页"
page.FirstPageTag = "首页"
page.LastPageTag = "尾页"
pageStr := page.FirstPage()
pageStr += page.PrevPage()
pageStr += page.PageBar("current")
pageStr += page.NextPage()
pageStr += page.LastPage()
return pageStr
}
2018-04-22 11:26:19 +08:00
return ""
}
// 为指定的页面返回地址值
func (page *Page) GetUrl(pageNo int) string {
2018-08-06 22:38:55 +08:00
// 复制一个URL对象
url := *page.Url
2018-08-06 22:38:55 +08:00
if len(page.UrlTemplate) == 0 && page.Router != nil {
page.UrlTemplate = page.makeUrlTemplate(url.Path, page.Router)
}
if len(page.UrlTemplate) > 0 {
// 指定URL生成模板
url.Path = gstr.Replace(page.UrlTemplate, "{.page}", gconv.String(pageNo))
return url.String()
}
2018-08-06 22:38:55 +08:00
values := page.Url.Query()
values.Set(page.PageName, gconv.String(pageNo))
url.RawQuery = values.Encode()
return url.String()
}
// 根据当前URL以及注册路由信息计算出对应的URL模板
func (page *Page) makeUrlTemplate(url string, router *ghttp.Router) (tpl string) {
if page.Router != nil && len(router.RegNames) > 0 {
if match, err := gregex.MatchString(router.RegRule, url); err == nil && len(match) > 0 {
if len(match) > len(router.RegNames) {
tpl = router.Uri
hasPageName := false
for i, name := range router.RegNames {
rule := fmt.Sprintf(`[:\*]%s|\{%s\}`, name, name)
if !hasPageName && strings.Compare(name, page.PageName) == 0 {
hasPageName = true
tpl, _ = gregex.ReplaceString(rule, `{.page}`, tpl)
} else {
tpl, _ = gregex.ReplaceString(rule, match[i + 1], tpl)
}
2018-07-30 10:28:07 +08:00
}
2018-08-06 22:38:55 +08:00
if !hasPageName {
tpl = ""
}
}
}
}
2018-08-06 22:38:55 +08:00
return
2018-04-22 11:26:19 +08:00
}
// 获取链接地址
func (page *Page) GetLink(url, text, title, style string) string {
2018-04-22 11:26:19 +08:00
if len(style) > 0 {
style = fmt.Sprintf(`class="%s" `, style)
2018-04-22 11:26:19 +08:00
}
if len(page.AjaxActionName) > 0 {
return fmt.Sprintf(`<a %shref='#' onclick="%s('%s')">%s</a>`, style, page.AjaxActionName, url, text)
2018-04-22 11:26:19 +08:00
} else {
return fmt.Sprintf(`<a %shref="%s" title="%s">%s</a>`, style, url, title, text)
2018-04-22 11:26:19 +08:00
}
2018-04-22 10:07:24 +08:00
}