pagination

package module
v0.0.0-...-4696331 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2015 License: MIT Imports: 4 Imported by: 0

README

Build Status Coverage Godoc license

Pagination

Pagination is a simple package that provides pagination. Has HTML support using the html/template package.

Installation

Install using go get.

$ go get github.com/SayonAB/pagination

Then import it in your project.

import "github.com/SayonAB/pagination"

Usage

Create a basic pagination and check the offset for current page

numPosts := db.Posts().Count()
postsPerPage := 25
currentPage := request.Query().Int("page")
p := pagination.New(numPosts, postsPerPage, currentPage)
fmt.Printf("The current offset is: %d\n", p.Offset()) // The current offset is: 75

Create a HTML pagination and use it in a html/template

p := pagination.NewHTML(110, 25, 2)
data := map[string]interface{}{
  "Pager": p,
}
var out bytes.Buffer
t := template.Must(template.New("pagination-test").Parse("{{ .Pager.Render }}"))
t.Execute(&out, data)
fmt.Printf("HTML list: %s\n", out.String())

Testing

Run the tests using go test.

$ go test

Contributing

All contributions are welcome! See CONTRIBUTING for more info.

License

Licensed under MIT license. See LICENSE for more information.

Documentation

Overview

Package pagination provides basic pagination tools. It contains a general implementation, also contains a implementation suitable for web pages as it can output html.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HTML

type HTML struct {
	*Pagination
}

HTML is a specialised paginator that also knows how to represent the pagination as a HTML list.

func NewHTML

func NewHTML(numberOfItems, itemsPerPage, currentPage int) *HTML

NewHTML returns a new html pagination with the provided values.

func (*HTML) Render

func (h *HTML) Render() template.HTML

Render returns the HTML representation of the pagination.

type Pagination

type Pagination struct {
	// contains filtered or unexported fields
}

Pagination is a general purpose pagination type, it knows how to calculate offset and number of pages. It also contains some utility functions that helps common tasks. One special utility is the PagesStream method that returns a channel to range over for presenting a list of all pages without adding them all to a slice.

func New

func New(numberOfItems, itemsPerPage, currentPage int) *Pagination

New returns a new Pagination with the provided values. The current page is normalized to be inside the bounds of the available pages. So if the current page supplied is less than 1 the current page is normalized as 1, and if it is larger than the number of pages needed its normalized as the last available page.

func (*Pagination) CurrentPage

func (p *Pagination) CurrentPage() int

CurrentPage returns the current page.

func (*Pagination) IsCurrentPage

func (p *Pagination) IsCurrentPage(page int) bool

IsCurrentPage checks a number to see if it matches the current page.

func (*Pagination) ItemsPerPage

func (p *Pagination) ItemsPerPage() int

ItemsPerPage returns the number of items to show per page.

func (*Pagination) NextPage

func (p *Pagination) NextPage() int

NextPage returns the page number of the page after current page. If current page is the last in the list of pages, the last page number is returned.

func (*Pagination) NumberOfItems

func (p *Pagination) NumberOfItems() int

NumberOfItems returns the number of items.

func (*Pagination) NumberOfPages

func (p *Pagination) NumberOfPages() int

NumberOfPages calculates the number of pages needed based on number of items and items per page.

func (*Pagination) Offset

func (p *Pagination) Offset() int

Offset calculates the offset into the collection the current page represents.

func (*Pagination) Pages

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

Pages returns a list with all page numbers. Eg. [1 2 3 4 5]

func (*Pagination) PagesStream

func (p *Pagination) PagesStream() chan int

PagesStream returns a channel that will be incremented to the available number of pages. Useful to range over when building a list of pages.

Example
package main

import (
	"fmt"

	"github.com/SayonAB/pagination"
)

func main() {
	p := pagination.New(110, 25, 1)
	pages := make([]int, 0, 5)
	for i := range p.PagesStream() {
		pages = append(pages, i)
	}
	fmt.Printf("%v", pages)
}
Output:

[1 2 3 4 5]

func (*Pagination) PreviousPage

func (p *Pagination) PreviousPage() int

PreviousPage returns the page number of the page before current page. If current page is the first in the list of pages, 1 is returned.

func (*Pagination) Show

func (p *Pagination) Show() bool

Show returns true if the pagination should be used. Ie. if there is more than one page.

Jump to

Keyboard shortcuts

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