diff --git a/util/gpage/gpage.go b/util/gpage/gpage.go index 01b4e7459..e448ce080 100644 --- a/util/gpage/gpage.go +++ b/util/gpage/gpage.go @@ -9,125 +9,93 @@ package gpage import ( "fmt" - "math" - "net/url" - "strings" - - "github.com/gogf/gf/net/ghttp" - "github.com/gogf/gf/text/gregex" "github.com/gogf/gf/text/gstr" "github.com/gogf/gf/util/gconv" + "math" ) // Page is the pagination implementer. +// All the attributes are public, you can change them when necessary. type Page struct { - UrlTemplate string // Custom url template for page url producing. TotalSize int // Total size. TotalPage int // Total page, which is automatically calculated. CurrentPage int // Current page number >= 1. - PageName string // Page variable name. It's "page" in default. + UrlTemplate string // Custom url template for page url producing. + LinkStyle string // CSS style name for HTML link tag . + SpanStyle string // CSS style name for HTML span tag , which is used for first, current and last page tag. + SelectStyle string // CSS style name for HTML select tag ` + barContent := fmt.Sprintf(`" - return ret + barContent += "" + return barContent } -// 预定义的分页显示风格内容 +// GetContent returns the page content for predefined mode. +// These predefined contents are mainly for chinese localization purpose. You can defines your own +// page function retrieving the page content according to the implementation of this function. func (p *Page) GetContent(mode int) string { switch mode { case 1: @@ -200,7 +171,7 @@ func (p *Page) GetContent(mode int) string { p.LastPageTag = "尾页" pageStr := p.FirstPage() pageStr += p.PrevPage() - pageStr += p.PageBar("current") + pageStr += p.PageBar() pageStr += p.NextPage() pageStr += p.LastPage() pageStr += fmt.Sprintf( @@ -218,7 +189,7 @@ func (p *Page) GetContent(mode int) string { p.LastPageTag = "尾页" pageStr := p.FirstPage() pageStr += p.PrevPage() - pageStr += p.PageBar("current") + pageStr += p.PageBar() pageStr += p.NextPage() pageStr += p.LastPage() return pageStr @@ -226,28 +197,24 @@ func (p *Page) GetContent(mode int) string { return "" } -// 为指定的页面返回地址值 -func (p *Page) GetUrl(pageNo int) string { - pattern := fmt.Sprintf(`(:%s|\*%s|\.%s)`, p.PageName, p.PageName, p.PageName) - result, _ := gregex.ReplaceString(pattern, pageNo, p.UrlTemplate) - url.Path = gstr.Replace(p.UrlTemplate, "{.page}", gconv.String(pageNo)) - return url.String() - } - - values := p.Url.Query() - values.Set(p.PageName, gconv.String(pageNo)) - url.RawQuery = values.Encode() - return url.String() +// GetUrl parses the UrlTemplate with given page number and returns the URL string. +// Note that the UrlTemplate attribute can be either an URL or a URI string with "{.page}" +// place holder specifying the page number position. +func (p *Page) GetUrl(page int) string { + return gstr.Replace(p.UrlTemplate, "{.page}", gconv.String(page)) } -// 获取链接地址 -func (p *Page) GetLink(url, text, title, style string) string { - if len(style) > 0 { - style = fmt.Sprintf(`class="%s" `, style) - } +// GetLink returns the HTML link tag content for given page number. +func (p *Page) GetLink(page int, text, title string) string { if len(p.AjaxActionName) > 0 { - return fmt.Sprintf(`%s`, style, p.AjaxActionName, url, text) + return fmt.Sprintf( + `%s`, + p.LinkStyle, p.AjaxActionName, p.GetUrl(page), title, text, + ) } else { - return fmt.Sprintf(`%s`, style, url, title, text) + return fmt.Sprintf( + `%s`, + p.LinkStyle, p.GetUrl(page), title, text, + ) } } diff --git a/util/gpage/gpage_unit_test.go b/util/gpage/gpage_unit_test.go new file mode 100644 index 000000000..f1a6d9196 --- /dev/null +++ b/util/gpage/gpage_unit_test.go @@ -0,0 +1,116 @@ +// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// 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. + +// go test *.go -bench=".*" + +package gpage_test + +import ( + "github.com/gogf/gf/util/gpage" + "testing" + + "github.com/gogf/gf/test/gtest" +) + +func Test_New(t *testing.T) { + gtest.Case(t, func() { + page := gpage.New(9, 2, 1, `/user/list?page={.page}`) + gtest.Assert(page.TotalSize, 9) + gtest.Assert(page.TotalPage, 5) + gtest.Assert(page.CurrentPage, 1) + }) + gtest.Case(t, func() { + page := gpage.New(9, 2, 0, `/user/list?page={.page}`) + gtest.Assert(page.TotalSize, 9) + gtest.Assert(page.TotalPage, 5) + gtest.Assert(page.CurrentPage, 1) + }) +} + +func Test_Basic(t *testing.T) { + gtest.Case(t, func() { + page := gpage.New(9, 2, 1, `/user/list?page={.page}`) + gtest.Assert(page.NextPage(), `>`) + gtest.Assert(page.PrevPage(), `<`) + gtest.Assert(page.FirstPage(), `|<`) + gtest.Assert(page.LastPage(), `>|`) + gtest.Assert(page.PageBar(), `12345`) + }) + + gtest.Case(t, func() { + page := gpage.New(9, 2, 3, `/user/list?page={.page}`) + gtest.Assert(page.NextPage(), `>`) + gtest.Assert(page.PrevPage(), `<`) + gtest.Assert(page.FirstPage(), `|<`) + gtest.Assert(page.LastPage(), `>|`) + gtest.Assert(page.PageBar(), `12345`) + }) + + gtest.Case(t, func() { + page := gpage.New(9, 2, 5, `/user/list?page={.page}`) + gtest.Assert(page.NextPage(), `>`) + gtest.Assert(page.PrevPage(), `<`) + gtest.Assert(page.FirstPage(), `|<`) + gtest.Assert(page.LastPage(), `>|`) + gtest.Assert(page.PageBar(), `12345`) + }) +} + +func Test_CustomTag(t *testing.T) { + gtest.Case(t, func() { + page := gpage.New(5, 1, 2, `/user/list/{.page}`) + page.PrevPageTag = "《" + page.NextPageTag = "》" + page.FirstPageTag = "|《" + page.LastPageTag = "》|" + page.PrevBarTag = "《《" + page.NextBarTag = "》》" + gtest.Assert(page.NextPage(), ``) + gtest.Assert(page.PrevPage(), ``) + gtest.Assert(page.FirstPage(), `|《`) + gtest.Assert(page.LastPage(), `》|`) + gtest.Assert(page.PageBar(), `12345`) + }) +} + +func Test_CustomStyle(t *testing.T) { + gtest.Case(t, func() { + page := gpage.New(5, 1, 2, `/user/list/{.page}`) + page.LinkStyle = "MyPageLink" + page.SpanStyle = "MyPageSpan" + page.SelectStyle = "MyPageSelect" + gtest.Assert(page.NextPage(), `>`) + gtest.Assert(page.PrevPage(), `<`) + gtest.Assert(page.FirstPage(), `|<`) + gtest.Assert(page.LastPage(), `>|`) + gtest.Assert(page.PageBar(), `12345`) + gtest.Assert(page.SelectBar(), ``) + }) +} + +func Test_Ajax(t *testing.T) { + gtest.Case(t, func() { + page := gpage.New(5, 1, 2, `/user/list/{.page}`) + page.AjaxActionName = "LoadPage" + gtest.Assert(page.NextPage(), `>`) + gtest.Assert(page.PrevPage(), `<`) + gtest.Assert(page.FirstPage(), `|<`) + gtest.Assert(page.LastPage(), `>|`) + gtest.Assert(page.PageBar(), `12345`) + }) +} + +func Test_PredefinedContent(t *testing.T) { + gtest.Case(t, func() { + page := gpage.New(5, 1, 2, `/user/list/{.page}`) + page.AjaxActionName = "LoadPage" + gtest.Assert(page.GetContent(1), `上一页 2 下一页`) + gtest.Assert(page.GetContent(2), `首页<<上一页[第2页]下一页>>尾页页`) + gtest.Assert(page.GetContent(3), `首页上一页12345下一页尾页当前页2/5 共5条`) + gtest.Assert(page.GetContent(4), `首页上一页12345下一页尾页`) + gtest.Assert(page.GetContent(5), ``) + }) +}