pagination

package
v1.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2015 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

The pagination package provides utilities to setup a paginator within the 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
    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>
    {{else}}
        <li class="disabled"><a>{{ i18n .Lang "paginator.first_page"}}</a></li>
        <li class="disabled"><a>&laquo;</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}}">&raquo;</a></li>
        <li><a href="{{.paginator.PageLinkLast}}">{{ i18n .Lang "paginator.last_page"}}</a></li>
    {{else}}
        <li class="disabled"><a>&raquo;</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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToInt64

func ToInt64(value interface{}) (d int64, err error)

convert any numeric value to int64

Types

type Paginator

type Paginator struct {
	Request     *http.Request
	PerPageNums int
	MaxPages    int
	// contains filtered or unexported fields
}

Paginator within the state of a http request.

func NewPaginator

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

Instantiates a paginator struct for the current http request.

func SetPaginator

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

Instantiates a Paginator and assigns it to context.Input.Data["paginator"].

func (*Paginator) HasNext

func (p *Paginator) HasNext() bool

Returns true if the current page has a successor.

func (*Paginator) HasPages

func (p *Paginator) HasPages() bool

Returns true if there is more than one page.

func (*Paginator) HasPrev

func (p *Paginator) HasPrev() bool

Returns true if the current page has a predecessor.

func (*Paginator) IsActive

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

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

func (*Paginator) Nums

func (p *Paginator) Nums() int64

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

func (*Paginator) Offset

func (p *Paginator) Offset() int

Returns the current offset.

func (*Paginator) Page

func (p *Paginator) Page() int

Returns the current page.

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

Returns URL for a given page index.

func (*Paginator) PageLinkFirst

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

Returns URL to the first page.

func (*Paginator) PageLinkLast

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

Returns URL to the last page.

func (*Paginator) PageLinkNext

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

Returns URL to the next page.

func (*Paginator) PageLinkPrev

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

Returns URL to the previous page.

func (*Paginator) PageNums

func (p *Paginator) PageNums() int

Returns the total number of pages.

func (*Paginator) Pages

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

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>
  </li>
{{end}}

func (*Paginator) SetNums

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

Sets the total number of items.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL