beego: Index | Files

package pagination

import ""

Package pagination provides utilities to setup a paginator within the context of a http request.


In your beego.Controller:

package controllers

import ""

type PostsController struct {

func (this *PostsController) ListAllPosts() {
    // sets this.Data["paginator"] with the current offset (from the url query param)
    postsPerPage := 20
    paginator := pagination.SetPaginator(this.Ctx, postsPerPage, CountPosts())

    // 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}}">&laquo;</a></li>
        <li class="disabled"><a>{{ i18n .Lang "paginator.first_page"}}</a></li>
        <li class="disabled"><a>&laquo;</a></li>
    {{range $index, $page := .paginator.Pages}}
        <li{{if $.paginator.IsActive .}} class="active"{{end}}>
            <a href="{{$.paginator.PageLink $page}}">{{$page}}</a>
    {{if .paginator.HasNext}}
        <li><a href="{{.paginator.PageLinkNext}}">&raquo;</a></li>
        <li><a href="{{.paginator.PageLinkLast}}">{{ i18n .Lang "paginator.last_page"}}</a></li>
        <li class="disabled"><a>&raquo;</a></li>
        <li class="disabled"><a>{{ i18n .Lang "paginator.last_page"}}</a></li>

See also


Package Files

controller.go doc.go paginator.go

type Paginator Uses

type Paginator pagination.Paginator

Paginator within the state of a http request.

func NewPaginator Uses

func NewPaginator(req *http.Request, per int, nums interface{}) *Paginator

NewPaginator Instantiates a paginator struct for the current http request.

func SetPaginator Uses

func SetPaginator(ctx *context.Context, per int, nums int64) (paginator *Paginator)

SetPaginator Instantiates a Paginator and assigns it to context.Input.Data("paginator").

func (*Paginator) HasNext Uses

func (p *Paginator) HasNext() bool

HasNext Returns true if the current page has a successor.

func (*Paginator) HasPages Uses

func (p *Paginator) HasPages() bool

HasPages Returns true if there is more than one page.

func (*Paginator) HasPrev Uses

func (p *Paginator) HasPrev() bool

HasPrev Returns true if the current page has a predecessor.

func (*Paginator) IsActive Uses

func (p *Paginator) IsActive(page int) bool

IsActive Returns true if the given page index points to the current page.

func (*Paginator) Nums Uses

func (p *Paginator) Nums() int64

Nums Returns the total number of items (e.g. from doing SQL count).

func (*Paginator) Offset Uses

func (p *Paginator) Offset() int

Offset Returns the current offset.

func (*Paginator) Page Uses

func (p *Paginator) Page() int

Page Returns the current page.

func (p *Paginator) PageLink(page int) string

PageLink Returns URL for a given page index.

func (*Paginator) PageLinkFirst Uses

func (p *Paginator) PageLinkFirst() (link string)

PageLinkFirst Returns URL to the first page.

func (*Paginator) PageLinkLast Uses

func (p *Paginator) PageLinkLast() (link string)

PageLinkLast Returns URL to the last page.

func (*Paginator) PageLinkNext Uses

func (p *Paginator) PageLinkNext() (link string)

PageLinkNext Returns URL to the next page.

func (*Paginator) PageLinkPrev Uses

func (p *Paginator) PageLinkPrev() (link string)

PageLinkPrev Returns URL to the previous page.

func (*Paginator) PageNums Uses

func (p *Paginator) PageNums() int

PageNums Returns the total number of pages.

func (*Paginator) Pages Uses

func (p *Paginator) Pages() []int

Pages Returns a list of all pages.

Usage (in a view template):

{{range $index, $page := .paginator.Pages}}
  <li{{if $.paginator.IsActive .}} class="active"{{end}}>
    <a href="{{$.paginator.PageLink $page}}">{{$page}}</a>

func (*Paginator) SetNums Uses

func (p *Paginator) SetNums(nums interface{})

SetNums Sets the total number of items.

Package pagination imports 5 packages (graph). Updated 2020-11-07. Refresh now. Tools for package owners.