pagination

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2020 License: MIT Imports: 3 Imported by: 0

README

Pagination

Build Status Coverage Status Go.Dev reference Go Report Card Release

Usage

import "clevergo.tech/pagination"
// query entities from database.
func query(limit, offset int64) (total int64, es []entity, err error) {
    return
}

func index(w http.ResponseWriter, req *http.Request) {
    p := pagination.NewFromRequest(req)
    p.Total, p.Items, _ = query(p.Limit, p.Offset())
    data, _ := json.Marshal(p)
    w.Write(data)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// PageParam defines the name of page parameter.
	PageParam = "page"
	// LimitParam defines the name of limit parameter.
	LimitParam = "limit"
	// MaxLimit defines a maximum number of limitation.
	MaxLimit int64 = 1000
	// DefaultPage defines the default value of page.
	DefaultPage int64 = 1
	// DefaultLimit defines the default value of limit.
	DefaultLimit int64 = 20
)

Functions

This section is empty.

Types

type Pagination

type Pagination struct {
	Page  int64       `json:"page"`
	Limit int64       `json:"limit"`
	Total int64       `json:"total"`
	Items interface{} `json:"items"`
}

Pagination represents a paginated list of data items.

func New

func New(page, limit int64) *Pagination

New returns a pagination with the given page and limit.

func NewFromContext

func NewFromContext(ctx *clevergo.Context) *Pagination

NewFromContext returns a pagination from the given context.

func NewFromRequest

func NewFromRequest(req *http.Request) *Pagination

NewFromRequest returns a pagination from the given HTTP request.

func (*Pagination) Offset

func (p *Pagination) Offset() int64

Offset returns the offset.

func (*Pagination) PageCount

func (p *Pagination) PageCount() int64

PageCount returns the number of pages.

func (*Pagination) Pages added in v0.2.0

func (p *Pagination) Pages() (pages []int64)

Pages returns a set of page numbers for rendering pagination. Zero means dots.

func (*Pagination) UnsignedLimit added in v0.3.0

func (p *Pagination) UnsignedLimit() uint64

UnsignedLimit returns the unsigned limit.

func (*Pagination) UnsignedOffset added in v0.3.0

func (p *Pagination) UnsignedOffset() uint64

UnsignedOffset returns the unsigned offset.

func (*Pagination) UnsignedPage added in v0.3.0

func (p *Pagination) UnsignedPage() uint64

UnsignedPage returns the unsigned page.

func (*Pagination) UnsignedPageCount added in v0.3.0

func (p *Pagination) UnsignedPageCount() uint64

UnsignedPageCount returns the unsigned number of pages.

Jump to

Keyboard shortcuts

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