Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

pager source pure

Package pager provides pagination functionality through a generic pager implementation.

Overview

Package pager provides pagination functionality through a generic pager implementation.

Example usage:

Example
 1import (
 2    "strconv"
 3    "strings"
 4
 5    "gno.land/p/jeronimoalbi/pager"
 6)
 7
 8func Render(path string) string {
 9    // Define the items to paginate
10    items := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
11
12    // Create a pager that paginates 4 items at a time
13    p, err := pager.New(path, len(items), pager.WithPageSize(4))
14    if err != nil {
15        panic(err)
16    }
17
18    // Render items for the current page
19    var output strings.Builder
20    p.Iterate(func(i int) bool {
21        output.WriteString("- " + strconv.Itoa(items[i]) + "\n")
22        return false
23    })
24
25    // Render page picker
26    if p.HasPages() {
27        output.WriteString("\n" + pager.Picker(p))
28    }
29
30    return output.String()
31}

Constants 1

Variables 1

Functions 5

func Picker

1func Picker(p Pager) string
source

Picker returns a string with the pager as Markdown. An empty string is returned when the pager has no pages.

func MustNew

1func MustNew(rawURL string, totalItems int, options ...PagerOption) Pager
source

MustNew creates a new pager or panics if there is an error.

func New

1func New(rawURL string, totalItems int, options ...PagerOption) (Pager, error)
source

New creates a new pager.

func WithPageQueryParam

1func WithPageQueryParam(name string) PagerOption
source

WithPageQueryParam assigns the name of the URL query param for the page value.

func WithPageSize

1func WithPageSize(size int) PagerOption
source

WithPageSize assigns a page size to a pager.

Types 3

type Pager

struct
1type Pager struct {
2	query, pageQueryParam                 string
3	pageSize, page, pageCount, totalItems int
4}
source

Pager allows paging items.

Methods on Pager

func GetPageURI

method on Pager
1func (p Pager) GetPageURI(page int) string
source

GetPageURI returns the URI for a page. An empty string is returned when page doesn't exist.

func HasPages

method on Pager
1func (p Pager) HasPages() bool
source

HasPages checks if pager has more than one page.

func Iterate

method on Pager
1func (p Pager) Iterate(fn PagerIterFn) bool
source

Iterate allows iterating page items.

func NextPageURI

method on Pager
1func (p Pager) NextPageURI() string
source

NextPageURI returns the URI path to the next page. An empty string is returned when current page is the last page.

func Offset

method on Pager
1func (p Pager) Offset() int
source

Offset returns the index of the first page item.

func Page

method on Pager
1func (p Pager) Page() int
source

Page returns the current page number.

func PageCount

method on Pager
1func (p Pager) PageCount() int
source

PageCount returns the number pages.

func PageSize

method on Pager
1func (p Pager) PageSize() int
source

PageSize returns the size of each page.

func PrevPageURI

method on Pager
1func (p Pager) PrevPageURI() string
source

PrevPageURI returns the URI path to the previous page. An empty string is returned when current page is the first page.

func TotalItems

method on Pager
1func (p Pager) TotalItems() int
source

TotalItems returns the total number of items to paginate.

type PagerIterFn

func
1type PagerIterFn func(index int) (stop bool)
source

PagerIterFn defines a callback to iterate page items.

type PagerOption

func
1type PagerOption func(*Pager)
source

PagerOption configures the pager.

Imports 5

  • errors stdlib
  • math stdlib
  • net/url stdlib
  • strconv stdlib
  • strings stdlib

Source Files 4