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 ¶
- type HTML
- type Pagination
- func (p *Pagination) CurrentPage() int
- func (p *Pagination) IsCurrentPage(page int) bool
- func (p *Pagination) ItemsPerPage() int
- func (p *Pagination) NextPage() int
- func (p *Pagination) NumberOfItems() int
- func (p *Pagination) NumberOfPages() int
- func (p *Pagination) Offset() int
- func (p *Pagination) Pages() []int
- func (p *Pagination) PagesStream() chan int
- func (p *Pagination) PreviousPage() int
- func (p *Pagination) Show() bool
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.
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.