Go Language (Golang) fast implementation of a bootstrap-style page link

Source: Internet
Author: User
Tags sprintf
This is a creation in Article, where the information may have evolved or changed.

Write a rough, direct write dead link style, code can be further encapsulated, add some custom configuration items

The code is as follows:

Package Utilsimport ("FMT" "Math" "net/http" "Net/url" "StrConv" "strings")//pagination pager type pagination struct { Request *http. Requesttotal intpernum int}//newpagination New Page splitter func newpagination (req *http. Request, total int, pernum int) *pagination {return &pagination{request:req,total:total,pernum:pernum,}}//pages Rendering generates HTML paging tags func (P *pagination) Pages () string {queryparams: = P.request.url.query ()//Get pagepage from the current request: = Queryparams.get ("page") if page = = "" {page = "1"}//converts the page number to Integer to calculate pagenum, _: = StrConv. Atoi (page) If Pagenum = = 0 {return ""}//calculates the total number of pages var totalpagenum = int (math. Ceil (Float64 (p.total)/float64 (P.pernum))//Home link var firstLink string//previous link var prevlink string//next link var nextLink string//last link var lastlink string//median page link var pagelinks []string//Home and previous link if pagenum > 1 {firstLink = FMT. Sprintf (' <li><a href= '%s ' > Home </a></li> ', P.pageurl ("1")) Prevlink = Fmt. Sprintf (' <li><a href= '%s ' > Prev </a></li> ', P.pageurl (StrConv. Itoa (Pagenum-1))} else {firstLink = ' <li class= "disabled" ><a href= "#" > Home </a></li> ' prevlink = ' <li class= "Disabled" ><a href= "#" > Prev </a></li> '}//last and Next if Pagenum < totalpagenum {Lastlink = fmt. Sprintf (' <li><a href= '%s ' > Last </a></li> ', P.pageurl (StrConv. Itoa (totalpagenum)) NextLink = Fmt. Sprintf (' <li><a href= '%s ' > Next </a></li> ', P.pageurl (StrConv. Itoa (pagenum+1))} else {lastlink = ' <li class= ' disabled ' ><a href= ' # ' > End </a></li> ' nextLink = ' <li class= "Disabled" ><a href= "#" > Next </a></li> '}//generate an intermediate page link pagelinks = make ([]string, 0, 10) Startpos: = Pagenum-3endpos: = pagenum + 3if Startpos < 1 {endpos = endpos + int (math. Abs (Float64 (startpos))) + 1startPos = 1}if endpos > totalpagenum {endpos = totalpagenum}for i: = startpos; I <= Endpos; i++ {var s stringif i = = Pagenum {s = fmt. Sprintf (' <li class= ' active ' ><a href= '%s ' >%d</a></li> ', P.pageurl (StrConv. Itoa (i)), I)} else {s = fmt. Sprintf (' <li><a href= '%s ' >%d</a></li> ', P.pageurl (StrConv. Itoa (i)), i)}pagelinks = append (Pagelinks, s)}return FMT. Sprintf (' <ul class= ' pagination ' >%s%s%s%s%s</ul> ', FirstLink, Prevlink, strings. Join (Pagelinks, ""), NextLink, lastlink)}//pageurl Raw ingredient page Urlfunc (P *pagination) pageurl (page string) string {// Creates a new URL object based on the current URL u, _: = URL. Parse (p.request.url.string ()) Q: = U.query () q.set ("page", page) U.rawquery = Q.encode () return u.string ()}

How to use:

package main...import "你项目的路径/utils"import "github.com/gin-gonic/gin"import "html/template"...func main() {    ...    r := gin.Default()    r.GET("/", func(c *gin.context){          //创建一个分页器,一万条数据,每页30条          pagination := utils.NewPagination(c.Request, 10000, 30)           //传到模板中需要转换成template.HTML类型,否则html代码会被转义          c.HTML(200,"你的模板",gin.H{"pages":template.HTML(pagination.Pages())})      })        }

Preview:

Related Article

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.