https://github.com/godcong/paginator
Paginator for go web developers
https://github.com/godcong/paginator
go page pagination paginator web
Last synced: 3 months ago
JSON representation
Paginator for go web developers
- Host: GitHub
- URL: https://github.com/godcong/paginator
- Owner: godcong
- Created: 2021-03-02T02:41:40.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-03-29T19:34:32.000Z (about 1 year ago)
- Last Synced: 2025-01-26T08:11:23.629Z (5 months ago)
- Topics: go, page, pagination, paginator, web
- Language: Go
- Homepage:
- Size: 62.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# paginator
## Paginator is a go-based page-turning extension package
## Usage
- Create the paginator object
```go
package exampleimport "github.com/godcong/paginator/v3"
func main() {
p := paginator.New() //create the paginator module for use
//you can create a paginator with a custom options
p := paginator.New(paginator.PerPageOption(30)) //paging 30 items per page
p := paginator.New(paginator.PerPageKeyOption("perPage")) //set per page key with option
p := paginator.New(paginator.PageKeyOption("page")) //set page key with option
p := paginator.New(paginator.PerPageOption(30),
paginator.PerPageKeyOption("perPage"), paginator.PageKeyOption("page")) //create the paginator module with all custom options//use the paginator
page, err := p.Parse(Queryable) //parse will return the current page data and error
}```
- Implement the Queryable interface
```
//Turnable at least 3 interfaces that need to be implemented
Queryable //return the Finder for paginator queryCounter //count the total data
Getter //find the data by page
//optional
Cloner //all the data
```- A Queryable example
```go
package exampleimport (
"context""github.com/godcong/paginator/v3"
)type pageExample struct {
query *Query
}func (p pageExample) Count(ctx context.Context) (int64, error) {
count, err := p.query.Count(ctx)
return int64(count), err
}func (p pageExample) Clone() paginator.Finder {
return p.query.Clone()
}func (p pageExample) Finder(parser paginator.Parser) paginator.Finder {
v := parser.FindValue("catch", "")
if v != "" {
p.query = p.query.Where(Cacth(v))
}
id := parser.FindValue("id", "")
if id != "" {
p.query = p.query.Where(page.IDEq(id))
}return p
}func main() {
//then use
p.SetDefaultQuery(&pageExample{})
page, err := p.Parse(paginator.NewHTTPParser(req))
//or
page, err := p.ParseWithQuery(paginator.NewHTTPParser(req), &pageExample)
}
```- Request from web
```
http://127.0.0.1/api/v0/example?per_page=20&page=xx&id=xx,
```result will like this:
```json
{
"current_page": 1,
"last_page": 1,
"per_page": 20,
"data": [
{
"id": "1",
"name": "test1"
},
{
"id": "2",
"name": "test2"
}
],
"total": 2,
"first_page_url": "127.0.0.1/api/v0/example?page=1&per_page=20",
"last_page_url": "127.0.0.1/api/v0/example?page=1&per_page=20",
"next_page_url": "",
"prev_page_url": "",
"path": "127.0.0.1/api/v0/example"
}
```