paginator

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2020 License: MIT Imports: 2 Imported by: 5 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Paginator

Go Report Card Build Status Coverage Status

A simple way to implement pagination in Golang.

Usage

package main

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/sqlite"
	"github.com/vcraescu/go-paginator"
	"github.com/vcraescu/go-paginator/adapter"
	"time"
)

type Post struct {
	ID          uint `gorm:"primary_key"`
	Title       string
	Body        string
	PublishedAt time.Time
}

func main() {
	db, err := gorm.Open("sqlite3", "my_db.db")
	if err != nil {
		panic(fmt.Errorf("db connection error: %s", err))
	}

	db.AutoMigrate(&Post{})
	
	var posts []Post
	
	q := db.Model(Post{}).Where("published_at > ?", time.Now())
	p := paginator.New(adapter.NewGORMAdapter(q), 10)
	p.SetPage(2)
	
	if err = p.Results(&posts); err != nil {
		panic(err)
	}
	
	for _, post := range posts {
		fmt.Println(post.Title)
	}
}

Some of other methods available:

p.HasNext()
p.HasPrev()
p.HasPages()
p.PageNums()

Adapters

An adapter must implement the Adapter interface which has 2 methods:

  • Nums - must return the number of results;
  • Slice - must retrieve a slice for an offset and length.

This way you can create your own adapter for any kind of data source you want to paginate.

type Adapter interface {
	Nums() int
	Slice(offset, length int, data interface{}) error
}
GORM Adapter

To paginate a GORM query builder.

q := db.Model(Post{}).Where("published_at > ?", time.Now())
p := paginator.New(adapter.NewGORMAdapter(q), 10)
Slice adapter

To paginate a slice.

var pages []int
p := paginator.New(adapter.NewSliceAdapter(pages), 10)

Views

View models contains all necessary logic to render the paginator inside a template.

DefaultView

Use it if you want to render a paginator similar to the one from Google search.

< Prev 2 3 4 5 6 7 8 9 10 11 Next >

func main() {
	db, err := gorm.Open("sqlite3", "my_db.db")
	if err != nil {
		panic(fmt.Errorf("db connection error: %s", err))
	}

	db.AutoMigrate(&Post{})
	
	var posts []Post
	
	q := db.Model(Post{}).Where("published_at > ?", time.Now())
	p := paginator.New(adapter.NewGORMAdapter(q), 10)
	p.SetPage(7)
	
	view := view.New(&p)
	
	fmt.Println(view.Pages()) // [2 3 4 5 6 7 8 9 10 11]
	fmt.Println(view.Next()) // 8
	fmt.Println(view.Prev()) // 6
	fmt.Println(view.Current()) // 7
}

TODO

  • More adapters

License

Paginator is licensed under the MIT License.

Documentation

Index

Constants

View Source
const DefaultMaxPerPage = 10

DefaultMaxPerPage default number of records per page

Variables

View Source
var ErrNoNextPage = errors.New("no next page")

ErrNoNextPage current page is last page

View Source
var ErrNoPrevPage = errors.New("no previous page")

ErrNoPrevPage current page is first page

Functions

This section is empty.

Types

type Adapter

type Adapter interface {
	Nums() int
	Slice(offset, length int, data interface{}) error
}

Adapter any adapter must implement this interface

type Paginator

type Paginator struct {
	// contains filtered or unexported fields
}

Paginator structure

func New

func New(adapter Adapter, maxPerPage int) Paginator

New paginator constructor

func (Paginator) HasNext

func (p Paginator) HasNext() bool

HasNext returns true if current page is not the last page

func (Paginator) HasPages

func (p Paginator) HasPages() bool

HasPages returns true if there is more than one page

func (Paginator) HasPrev

func (p Paginator) HasPrev() bool

HasPrev returns true if current page is not the first page

func (Paginator) NextPage

func (p Paginator) NextPage() (int, error)

NextPage returns next page number or ErrNoNextPage if current page is last page

func (*Paginator) Nums

func (p *Paginator) Nums() int

Nums returns the total number of records

func (Paginator) Page

func (p Paginator) Page() int

Page returns current page

func (Paginator) PageNums

func (p Paginator) PageNums() int

PageNums returns the total number of pages

func (Paginator) PrevPage

func (p Paginator) PrevPage() (int, error)

PrevPage returns previous page number or ErrNoPrevPage if current page is first page

func (Paginator) Results

func (p Paginator) Results(data interface{}) error

Results stores the current page results into data argument which must be a pointer to a slice.

func (*Paginator) SetPage

func (p *Paginator) SetPage(page int)

SetPage set current page

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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