A paging method for Golang

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

Need to make a page: Just made a method, the example in Beego is too troublesome. Let's get a simple one, on the code.

Pagination method, according to the number of pages passed over, per page, total, returns the contents of the pagination 7 pages before the 1,2,3,4,5 format returned, less than 5 pages return the specific page Func paginator (page, Prepage int, nums Int64) Map[stri ng]interface{} {var firstpage int//Previous page address var lastpage int//Next page address//number of nums per page, and prepage total page count totalpages: = Int (math. Ceil (Float64 (nums)/float64 (prepage))//page total if page > totalpages {page = totalpages}if page <= 0 {page = 1}var p Ages []intswitch {case page >= totalpages-5 && totalpages > 5://Last 5 page start: = totalpages-5 + 1firstpage = page-1lastpage = Int (math. Min (Float64 (totalpages), float64 (page+1))) pages = make ([]int, 5) for I, _: = Range pages {Pages[i] = start + i}case page &G t;= 3 && totalpages > 5:start: = page-3 + 1pages = make ([]int, 5) FirstPage = Page-3for I, _: = Range pages {Pages[i] = start + i}firstpage = page-1lastpage = page + 1default:pages = make ([]int, int (math. Min (5, Float64 (totalpages))))) for I, _: = Range pages {Pages[i] = i + 1}firstpage = Int (math. Max (float64 (1), float64 (page-1))) LastPage = page + 1//Fmt. PRINTLN (pages)}paginatormap: = Make (map[string]interface{}) paginatormap["pages"] = pagespaginatormap["TotalPages"] = totalpagespaginatormap["FirstPage"] = firstpagepaginatormap["lastpage"] = lastpagepaginatormap["currpage"] = Pagereturn Paginatormap}


The HTML is like this

  <div class= "AM-CF" >  total {{. Totals}} record shared {{. Paginator.totalpages}} page current page  {{. Paginator.currpage}  } <div class= "Am-fr" >    <ul class= "am-pagination" >    <li class= "" ><a href= "/clubadmin/ Topics/{{.paginator.firstpage}} ">«</a></li>     {{range $index, $page: =. paginator.pages}}      <li  {{if EQ $.paginator.currpage $page}}class= ' am-active ' {{end}}><a href= '/clubadmin/topics/{{$page}} ">{{$page}}</a></li>      {{end}}      <li><a href="/clubadmin/topics/{{. Paginator.lastpage}} ">»</a></li>    </ul>  </div></div>

It's like this in the controller.

Res: = models. Paginator (PA, pre_page, totals) this. data["paginator"] = Res



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.