pager source pure
Package pager provides pagination functionality through a generic pager implementation.
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}
1
1
5
func Picker
Picker returns a string with the pager as Markdown. An empty string is returned when the pager has no pages.
func MustNew
MustNew creates a new pager or panics if there is an error.
func New
New creates a new pager.
func WithPageQueryParam
WithPageQueryParam assigns the name of the URL query param for the page value.
func WithPageSize
WithPageSize assigns a page size to a pager.
3
type Pager
structPager allows paging items.
Methods on Pager
func GetPageURI
method on PagerGetPageURI returns the URI for a page. An empty string is returned when page doesn't exist.
func HasPages
method on PagerHasPages checks if pager has more than one page.
func Iterate
method on PagerIterate allows iterating page items.
func NextPageURI
method on PagerNextPageURI returns the URI path to the next page. An empty string is returned when current page is the last page.
func Offset
method on PagerOffset returns the index of the first page item.
func Page
method on PagerPage returns the current page number.
func PageCount
method on PagerPageCount returns the number pages.
func PageSize
method on PagerPageSize returns the size of each page.
func PrevPageURI
method on PagerPrevPageURI returns the URI path to the previous page. An empty string is returned when current page is the first page.
func TotalItems
method on PagerTotalItems returns the total number of items to paginate.
type PagerIterFn
funcPagerIterFn defines a callback to iterate page items.
type PagerOption
funcPagerOption configures the pager.
5
- errors stdlib
- math stdlib
- net/url stdlib
- strconv stdlib
- strings stdlib