Skip to content

jkbicbic/gopager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

49 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Gopager

GoDoc Go Report Card Maintainability Test Coverage

a small library for your golang project pagination needs.

Example Pagination

Below is how your pagination could look. Please note that any styles that you see here are not included, you and you alone have the power to design your own pagination this library just provides the calculations.

alt text

Installation

Adding the package from github

go get github.com/jkbicbic/gopager

Importing the package in your project

import "github.com/jkbicbic/gopager"

Usage

Gopager accepts 4 arguments

var count int = 40 // totalrows of your query
var pageLimit int = 3 // the number of rows for each page
var currentPage int = 5 // the current page
var pagerLength int = 3 // the length of your paginator e.g. [4 5 6] for size 3 [4 5 6 7 8] for size 5

Build your pagination using gopager.New()

p := gopager.New(count, pageLimit, currentPage, pagerLength)  // creates a new instance of pagination
p.Paginate()                                                  // builds the pagination

You can pass this in a map[string]interface{}{} to be used in your template

data := map[string]interface{}{}
data["paginator"] = p

sample template usage using bulma pagination

<nav class="pagination" role="navigation" aria-label="pagination">
  <ul class="pagination-list">
    {{if .paginator.HasPrev}}
    <li>
      <a class="pagination-link" href="?page={{.paginator.FirstPage}}"> First </a>
    </li>
    <li>
      <a class="pagination-link" rel="prev" href="?page={{.paginator.Prev}}"> Prev </a>
    </li>
    <li>
      <a class="pagination-link" href="?page={{.paginator.FirstPage}}">{{.paginator.FirstPage}}</a>
    </li>
    <li>
      <span class="pagination-ellipsis">&hellip;</span>
    </li>
    {{end}}
    {{$currentPage := .paginator.CurrentPage}}
    {{range $i := .paginator.Pages}}
    {{if eq $i $currentPage}}
    <li>
      <a class="pagination-link is-current">{{$i}}</a>
    </li>
    {{else}}
    <li>
      <a class="pagination-link" href="?page={{$i}}">{{$i}}</a>
    </li>
    {{end}}
    {{end}}
    {{if not .paginator.NearLast}}
    <li>
      <span class="pagination-ellipsis">&hellip;</span>
    </li>
    <li>
      <a class="pagination-link" href="?page={{.paginator.TotalPages}}">{{.paginator.TotalPages}}</a>
    </li>
    {{end}}
    {{if .paginator.HasNext}}
    <li>
      <a class="pagination-link" rel="next" href="?page={{.paginator.Next}}"> Next </a>
    </li>
    <li>
      <a class="pagination-link" href="?&page={{.paginator.TotalPages}}"> Last </a>
    </li>
    {{end}}
  </ul>
</nav>

Gopager struct for reference

type Pagination struct {
	HasPrev                 bool
	HasNext                 bool
	NearLast                bool
	Firstpage               int
	Prev                    int
	Next                    int
	Pages                   []int
	CurrentPage             int
	PagerStart              int
	TotalPages, pagerLength int
}

Contribute

create issues here

About

๐Ÿ“ƒ a small go library for pagination

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages