doc.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. Package pagination provides utilities to setup a paginator within the
  3. context of a http request.
  4. Usage
  5. In your beego.Controller:
  6. package controllers
  7. import "github.com/astaxie/beego/utils/pagination"
  8. type PostsController struct {
  9. beego.Controller
  10. }
  11. func (this *PostsController) ListAllPosts() {
  12. // sets this.Data["paginator"] with the current offset (from the url query param)
  13. postsPerPage := 20
  14. paginator := pagination.SetPaginator(this.Ctx, postsPerPage, CountPosts())
  15. // fetch the next 20 posts
  16. this.Data["posts"] = ListPostsByOffsetAndLimit(paginator.Offset(), postsPerPage)
  17. }
  18. In your view templates:
  19. {{if .paginator.HasPages}}
  20. <ul class="pagination pagination">
  21. {{if .paginator.HasPrev}}
  22. <li><a href="{{.paginator.PageLinkFirst}}">{{ i18n .Lang "paginator.first_page"}}</a></li>
  23. <li><a href="{{.paginator.PageLinkPrev}}">&laquo;</a></li>
  24. {{else}}
  25. <li class="disabled"><a>{{ i18n .Lang "paginator.first_page"}}</a></li>
  26. <li class="disabled"><a>&laquo;</a></li>
  27. {{end}}
  28. {{range $index, $page := .paginator.Pages}}
  29. <li{{if $.paginator.IsActive .}} class="active"{{end}}>
  30. <a href="{{$.paginator.PageLink $page}}">{{$page}}</a>
  31. </li>
  32. {{end}}
  33. {{if .paginator.HasNext}}
  34. <li><a href="{{.paginator.PageLinkNext}}">&raquo;</a></li>
  35. <li><a href="{{.paginator.PageLinkLast}}">{{ i18n .Lang "paginator.last_page"}}</a></li>
  36. {{else}}
  37. <li class="disabled"><a>&raquo;</a></li>
  38. <li class="disabled"><a>{{ i18n .Lang "paginator.last_page"}}</a></li>
  39. {{end}}
  40. </ul>
  41. {{end}}
  42. See also
  43. http://beego.me/docs/mvc/view/page.md
  44. */
  45. package pagination