Help us shape the future of Go! Take the 2026 Go Developer Survey

pagination

package module
v0.0.0-...-4d6e34a Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2017 License: MIT Imports: 1 Imported by: 0

README

go-pagination

Build Status GoDoc

a package for calculating values needed for pagination in golang

Installation

In order to start, go get this repository:

go get github.com/schwarmco/go-pagination

Usage

import (
    "fmt"
    "github.com/schwarmco/go-pagination"
)

items := []string{"apple", "cherry", "pear", "coconut", "banana"}

// assume, we are on page 1 and want 3 items listed on each page
pager := pagination.Calculate(1, 3, len(items))

fmt.Println(pager.NumPages) // Output: 2
fmt.Println(pager.HasNext) // Output: true
fmt.Println(pager.HasPrev) // Output: false

Todo

  • implement pagination range pattern (like: 1 2 3 .. 7 8 9 ... 12 13 14)

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Values

type Values struct {
	NumPages           int
	HasPrev, HasNext   bool
	PrevPage, NextPage int
	ItemsPerPage       int
	CurrentPage        int
	NumItems           int
}

func Calculate

func Calculate(currentPage, itemsPerPage, numItems int) (v Values)

pages start at 1 - not 0

Example
currentPage := 2
itemsPerPage := 10
numItems := 42

pager := Calculate(currentPage, itemsPerPage, numItems)

fmt.Println("NumPages:", pager.NumPages)
fmt.Println("HasPrev:", pager.HasPrev)
fmt.Println("PrevPage:", pager.PrevPage)
fmt.Println("HasNext:", pager.HasNext)
fmt.Println("NextPage:", pager.NextPage)
Output:
NumPages: 5
HasPrev: true
PrevPage: 1
HasNext: true
NextPage: 3

Jump to

Keyboard shortcuts

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