2014-10-07 09:39:27 +00:00
|
|
|
/*
|
2015-09-14 15:17:33 +00:00
|
|
|
Package pagination provides utilities to setup a paginator within the
|
2014-10-07 09:39:27 +00:00
|
|
|
context of a http request.
|
|
|
|
|
|
|
|
Usage
|
|
|
|
|
|
|
|
In your beego.Controller:
|
|
|
|
|
|
|
|
package controllers
|
|
|
|
|
|
|
|
import "github.com/astaxie/beego/utils/pagination"
|
|
|
|
|
|
|
|
type PostsController struct {
|
|
|
|
beego.Controller
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *PostsController) ListAllPosts() {
|
|
|
|
// sets this.Data["paginator"] with the current offset (from the url query param)
|
|
|
|
postsPerPage := 20
|
2014-10-08 13:59:26 +00:00
|
|
|
paginator := pagination.SetPaginator(this.Ctx, postsPerPage, CountPosts())
|
2014-10-07 09:39:27 +00:00
|
|
|
|
|
|
|
// fetch the next 20 posts
|
|
|
|
this.Data["posts"] = ListPostsByOffsetAndLimit(paginator.Offset(), postsPerPage)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
In your view templates:
|
|
|
|
|
|
|
|
{{if .paginator.HasPages}}
|
|
|
|
<ul class="pagination pagination">
|
|
|
|
{{if .paginator.HasPrev}}
|
|
|
|
<li><a href="{{.paginator.PageLinkFirst}}">{{ i18n .Lang "paginator.first_page"}}</a></li>
|
|
|
|
<li><a href="{{.paginator.PageLinkPrev}}">«</a></li>
|
|
|
|
{{else}}
|
|
|
|
<li class="disabled"><a>{{ i18n .Lang "paginator.first_page"}}</a></li>
|
|
|
|
<li class="disabled"><a>«</a></li>
|
|
|
|
{{end}}
|
|
|
|
{{range $index, $page := .paginator.Pages}}
|
|
|
|
<li{{if $.paginator.IsActive .}} class="active"{{end}}>
|
|
|
|
<a href="{{$.paginator.PageLink $page}}">{{$page}}</a>
|
|
|
|
</li>
|
|
|
|
{{end}}
|
|
|
|
{{if .paginator.HasNext}}
|
|
|
|
<li><a href="{{.paginator.PageLinkNext}}">»</a></li>
|
|
|
|
<li><a href="{{.paginator.PageLinkLast}}">{{ i18n .Lang "paginator.last_page"}}</a></li>
|
|
|
|
{{else}}
|
|
|
|
<li class="disabled"><a>»</a></li>
|
|
|
|
<li class="disabled"><a>{{ i18n .Lang "paginator.last_page"}}</a></li>
|
|
|
|
{{end}}
|
|
|
|
</ul>
|
|
|
|
{{end}}
|
|
|
|
|
|
|
|
See also
|
|
|
|
|
|
|
|
http://beego.me/docs/mvc/view/page.md
|
|
|
|
|
|
|
|
*/
|
|
|
|
package pagination
|