diff --git a/g/util/gpage/gpage.go b/g/util/gpage/gpage.go
index a0d449b31..9a8dafed9 100644
--- a/g/util/gpage/gpage.go
+++ b/g/util/gpage/gpage.go
@@ -8,71 +8,88 @@
package gpage
import (
- "gitee.com/johng/gf/g/net/ghttp"
- "math"
"fmt"
- "net/url"
- "gitee.com/johng/gf/g/util/gconv"
+ "math"
"strings"
+ url2 "net/url"
+ "gitee.com/johng/gf/g/util/gconv"
)
+// 分页对象
type Page struct {
- pageName string // 分页参数名称
- nextPageTag string // 下一页标签
- prevPageTag string // 上一页标签
- firstPageTag string // 首页标签
- lastPageTag string // 尾页标签
-
- prevBar string // 上一分页条
- nextBar string // 下一分页条
- formatLeft string
- formatRight string
- isAjax bool // 是否支持AJAX分页模式
- totalSize int
- pagebarNum int // 控制记录条的个数。
- totalPage int // 总页数
- ajaxActionName string // AJAX动作名
- currentPage int // 当前页
- url string // url地址头
- offset int
+ pageName string // 分页参数名称
+ nextPageTag string // 下一页标签
+ prevPageTag string // 上一页标签
+ firstPageTag string // 首页标签
+ lastPageTag string // 尾页标签
+ prevBar string // 上一分页条
+ nextBar string // 下一分页条
+ totalSize int // 总共条数
+ pageBarNum int // 控制记录条的个数
+ totalPage int // 总页数
+ currentPage int // 当前页
+ offset int // 分页的offset条数
+ url *url2.URL // URL对象
+ route string // 路由规则
+ ajaxActionName string // AJAX动作名,当该属性有值时,表示使用AJAX分页
}
-func New(totalSize, perPage, currentPage int, url string) *Page {
+// 创建一个分页对象,输入参数分别为:
+// 总数量、每页数量、当前页码、当前的URL(可以只是URI+QUERY)、(可选)路由规则(例如: /user/list/:page、/order/list/*page)
+func New(totalSize, perPage, currentPage int, url string, route...string) *Page {
+ u, _ := url2.Parse(url)
page := &Page {
- pageName : "page",
- totalSize : totalSize,
- totalPage : int(math.Ceil(float64(totalSize/perPage))),
- currentPage : currentPage,
- offset : (currentPage - 1)*perPage,
- url : url,
+ pageName : "page",
+ prevPageTag : "<",
+ nextPageTag : ">",
+ firstPageTag : "|<",
+ lastPageTag : ">|",
+ prevBar : "<<",
+ nextBar : ">>",
+ totalSize : totalSize,
+ totalPage : int(math.Ceil(float64(totalSize/perPage))),
+ currentPage : currentPage,
+ offset : (currentPage - 1)*perPage,
+ pageBarNum : 10,
+ url : u,
}
- if strings.Index(url, "?") != -1 {
- page.url = url + "&"
- } else {
- page.url = url + "?"
+ if len(route) > 0 {
+ page.route = route[0]
}
- page.url += page.pageName + "="
return page
}
// 启用AJAX分页
func (page *Page)EnableAjax(actionName string) {
- page.isAjax = true
page.ajaxActionName = actionName
}
// 获取显示"下一页"的内容.
-func (page *Page) nextPage(curStyle , style string) string {
+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)
+ return page.getLink(page.getUrl(page.currentPage + 1), page.nextPageTag, "下一页", style)
}
return fmt.Sprintf(`%s`, curStyle, page.nextPageTag)
}
/// 获取显示“上一页”的内容
-func (page *Page) prevPage(curStyle , style string) string {
+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)
+ return page.getLink(page.getUrl(page.currentPage - 1), page.prevPageTag, "上一页", style)
}
return fmt.Sprintf(`%s`, curStyle, page.prevPageTag)
}
@@ -82,43 +99,66 @@ func (page *Page) prevPage(curStyle , style string) string {
*
* @return string
*/
-func (page *Page)firstPage(curStyle, style string) 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(`%s`, curStyle, page.firstPageTag)
}
- return page._getLink(page._getUrl(1), page.firstPageTag, "第一页", style)
+ return page.getLink(page.getUrl(1), page.firstPageTag, "第一页", style)
}
// 获取显示“尾页”的内容
-func (page *Page)lastPage(curStyle, style string) string {
+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(`%s`, curStyle, page.lastPageTag)
}
- return page._getLink(page._getUrl(page.totalPage), page.lastPageTag, "最后页", style)
+ return page.getLink(page.getUrl(page.totalPage), page.lastPageTag, "最后页", style)
}
// 获得分页条。
-func (page *Page) nowBar(curStyle, style string) string {
- plus := int(math.Ceil(float64(page.pagebarNum / 2)))
- if page.pagebarNum - plus + page.currentPage > page.totalPage {
- plus = page.pagebarNum - page.totalPage + page.currentPage
+func (page *Page) nowBar(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
}
begin := page.currentPage - plus + 1
if begin < 1 {
begin = 1
}
ret := ""
- for i := begin; i < begin + page.pagebarNum; i++ {
+ for i := begin; i < begin + page.pageBarNum; i++ {
if i <= page.totalPage {
if i != page.currentPage {
- ret += page._getText(page._getLink(page._getUrl(i), gconv.String(i), style, ""))
+ ret += page.getLink(page.getUrl(i), gconv.String(i), style, "")
} else {
- ret += page._getText(fmt.Sprintf(`%d`, curStyle, i))
+ ret += fmt.Sprintf(`%d`, curStyle, i)
}
} else {
break
}
- ret += "\n"
+ if i != begin + page.pageBarNum - 1 {
+ ret += "\n"
+ }
}
return ret
}
@@ -128,8 +168,7 @@ func (page *Page) nowBar(curStyle, style string) string {
* @return string
*/
func (page *Page) selectBar() string {
- url := page._getUrl("' + this.value")
- ret := fmt.Sprintf(`