// Copyright 2018 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. // Package gpage provides useful paging functionality for web pages. package gpage import ( "fmt" "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 { TotalSize int // Total size. TotalPage int // Total page, which is automatically calculated. CurrentPage int // Current page number >= 1. 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 `, p.SelectStyle) for i := 1; i <= p.TotalPage; i++ { if i == p.CurrentPage { barContent += fmt.Sprintf(``, p.GetUrl(i), i) } else { barContent += fmt.Sprintf(``, p.GetUrl(i), i) } } 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: p.NextPageTag = "下一页" p.PrevPageTag = "上一页" return fmt.Sprintf( `%s %d %s`, p.PrevPage(), p.CurrentPage, p.NextPage(), ) case 2: p.NextPageTag = "下一页>>" p.PrevPageTag = "<<上一页" p.FirstPageTag = "首页" p.LastPageTag = "尾页" return fmt.Sprintf( `%s%s[第%d页]%s%s第%s页`, p.FirstPage(), p.PrevPage(), p.CurrentPage, p.NextPage(), p.LastPage(), p.SelectBar(), ) case 3: p.NextPageTag = "下一页" p.PrevPageTag = "上一页" p.FirstPageTag = "首页" p.LastPageTag = "尾页" pageStr := p.FirstPage() pageStr += p.PrevPage() pageStr += p.PageBar() pageStr += p.NextPage() pageStr += p.LastPage() pageStr += fmt.Sprintf( `当前页%d/%d 共%d条`, p.CurrentPage, p.TotalPage, p.TotalSize, ) return pageStr case 4: p.NextPageTag = "下一页" p.PrevPageTag = "上一页" p.FirstPageTag = "首页" p.LastPageTag = "尾页" pageStr := p.FirstPage() pageStr += p.PrevPage() pageStr += p.PageBar() pageStr += p.NextPage() pageStr += p.LastPage() return pageStr } return "" } // 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_PLACE_HOLDER, gconv.String(page)) } // 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`, p.LinkStyle, p.AjaxActionName, p.GetUrl(page), title, text, ) } else { return fmt.Sprintf( `%s`, p.LinkStyle, p.GetUrl(page), title, text, ) } }