This is a creation in Article, where the information may have evolved or changed.
Tool Packtools/paging.go
toolsimport ("math") Func createpaging (page, pagesize, total Int64) *paging {if page < 1 {page = 1}if PageSize < 1 {pagesize = 10}page_count: = Math. Ceil (Float64 (total)/float64 (pagesize)) Paging: = new (paging) paging. Page = pagepaging. Pagesize = pagesizepaging. Total = totalpaging. PageCount = Int64 (Page_count) paging. Numscount = 7paging.setnums () return paging}type paging struct {page Int64//Current page pagesize Int64//number of bars per page total in T64//Total number of PageCount Int64//Total pages Nums []int64//pagination ordinal numscount Int64//Total page ordinal}func (this *paging) setnums () {this. Nums = []int64{}if this. PageCount = = 0 {return}half: = Math. Floor (float64) (this. Numscount)/float64 (2)) Begin: = this. Page-int64 (half) if begin < 1 {begin = 1}end: = begin + this. Numscount-1if End >= this. PageCount {begin = this. Pagecount-this. Numscount + 1if begin < 1 {begin = 1}end = this. Pagecount}for I: = begin; I <= end; i++ {this. Nums = Append (this. Nums, I)}}
Used in the controllercontrollers/test.go
package controllersimport ("test/tools""strconv""github.com/astaxie/beego")type TestController struct {beego.Controller}func (this *TestController) Paging() {page, _ := this.GetInt64("page")pageSize, _ := this.GetInt64("pageSize")if page < 1 {page = 1}if pageSize < 1 {pageSize = 10}this.Data["paging"] = tools.CreatePaging(page, pageSize, 365)this.TplName = "test.html"}
Templateviews/test.html
<ul class="pagination"> <li> <a href="?page=1&pageSize={{$.paging.Pagesize}}" class="not">«</a> </li> {{range $k,$v:=.paging.Nums}} <li> <a href="?page={{$v}}&pageSize={{$.paging.Pagesize}}" class="{{if eq $v $.paging.Page}}active{{end}}">{{$v}}</a> </li> {{end}} <li> <a href="?page={{.paging.PageCount}}&pageSize={{$.paging.Pagesize}}">»</a> </li></ul>
Accesshttp://192.168.1.55:8080/test/paging?page=11&pageSize=10