Golang Sub-page

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.