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
- type Paginator
- func (p *Paginator) CurrentPage() int
- func (p *Paginator) IsCurrentPage(page int) bool
- func (p *Paginator) ItemsPerPage() int
- func (p *Paginator) NextPage() int
- func (p *Paginator) NumberOfItems() int
- func (p *Paginator) NumberOfPages() int
- func (p *Paginator) Offset() int
- func (p *Paginator) Pages() []int
- func (p *Paginator) PagesStream() chan int
- func (p *Paginator) PreviousPage() int
- func (p *Paginator) Show() bool
- func (p *Paginator) ToPagination() Pagination
- func (p *Paginator) ToPaginationWithData(slice interface{}) Pagination
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HTML ¶
type HTML struct {
*Paginator
}
HTML is a specialised paginator that also knows how to represent the pagination as a HTML list.
type Pagination ¶
type Pagination struct {
ItemsPerPage int `json:"per_page"`
NumberOfItems int `json:"total_entries"`
CurrentPage int `json:"page"`
Offset int `json:"offset"`
NextPage int `json:"next_page"`
PreviousPage int `json:"previous_page"`
TotalPages int `json:"total_pages"`
Data []interface{} `json:"data"`
}
Pagination is a public version of the paginator. It does not have any logic attached and can be easily serialized to JSON.
type Paginator ¶
type Paginator struct {
// contains filtered or unexported fields
}
Paginator 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 ¶
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 NewFromRequest ¶
NewFromRequest retusn a new Pagination with the provided values. However, unlike New, it uses an HTTP request to parse the page number to use.
func (*Paginator) CurrentPage ¶
CurrentPage returns the current page.
func (*Paginator) IsCurrentPage ¶
IsCurrentPage checks a number to see if it matches the current page.
func (*Paginator) ItemsPerPage ¶
ItemsPerPage returns the number of items to show per page.
func (*Paginator) NextPage ¶
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 (*Paginator) NumberOfItems ¶
NumberOfItems returns the number of items.
func (*Paginator) NumberOfPages ¶
NumberOfPages calculates the number of pages needed based on number of items and items per page.
func (*Paginator) Offset ¶
Offset calculates the offset into the collection the current page represents.
func (*Paginator) PagesStream ¶
PagesStream returns a channel that will be incremented to the available number of pages. Useful to range over when building a list of pages.
func (*Paginator) PreviousPage ¶
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 (*Paginator) Show ¶
Show returns true if the pagination should be used. Ie. if there is more than one page.
func (*Paginator) ToPagination ¶
func (p *Paginator) ToPagination() Pagination
ToPagination returns a Pagination instance which can be serialized and returned in API responses
func (*Paginator) ToPaginationWithData ¶
func (p *Paginator) ToPaginationWithData(slice interface{}) Pagination
ToPaginationWithData is like ToPagination but it also includes some arbitrary data, which usually ends up being the databa being paginated.