Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/knadh/paginator
Tiny Go package for pagination queries and generating page numbers
https://github.com/knadh/paginator
limit offset pagination pagination-generator pagination-library paginator
Last synced: 23 days ago
JSON representation
Tiny Go package for pagination queries and generating page numbers
- Host: GitHub
- URL: https://github.com/knadh/paginator
- Owner: knadh
- License: mit
- Created: 2019-08-05T12:53:55.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-06-03T10:47:23.000Z (over 1 year ago)
- Last Synced: 2024-06-18T21:35:47.465Z (5 months ago)
- Topics: limit, offset, pagination, pagination-generator, pagination-library, paginator
- Language: Go
- Size: 9.77 KB
- Stars: 20
- Watchers: 3
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# paginatior
paginator provides a simple abstracting for handling pagination requests and offset/limit generation for HTTP requests. The most common usecase is arbitrary queries that need to be paginated with query params coming in from a UI, for instance, /things/all?page=2&per_page=5. paginator can parse and sanitize these values and provide offset and limit values that can be passed to the database query there by avoiding boilerplate code for basic pagination. In addition, it can also generate HTML-ready page number series (Google search style).
## Features
- 0 boilerplate for reading pagination params from HTTP queries
- Automatic offset-limit calculator for DB queries
- Automatic sliding-window HTML pagination generation![image](https://user-images.githubusercontent.com/547147/62465979-d73f8400-b7ad-11e9-98a0-dece2aac5d57.png)
## Usage
```go
// Initialize global paginator instance.
pg := paginator.New(paginator.Default())// Get page query params from an HTTP request.
// The params to be picked up are defined in options
// set by .Default() above.
p := pg.NewFromURL(req.URL.Query())// or, pass page params directly, page and per_page.
p := pg.New(1, 20)// Query your database with p.Offset and p.Limit.
// Once you get the total number of results back
// from the database, do:
p.SetTotal(totalFromDB)// Generate HTML page numbers in a template
// (and pass optional query params to append to each page URL)
p.HTML("", url.Values{})
```## Example
Check out the [Alar dictionary glossary](https://alar.ink/glossary/kannada/english/%E0%B2%85) to see paginator in action.Licensed under the MIT license.