paginator

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

README

A simple way to implement pagination in Golang.

Usage

package main

import (
	"encoding/json"
	"encoding/xml"
	"fmt"
	"github.com/pkg6/go-paginator"
	"github.com/pkg6/go-paginator/adapter"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

type Post struct {
	ID     uint `gorm:"primarykey" json:"id"`
	Number int  `json:"number"`
}

var db, _ = gorm.Open(sqlite.Open("gorm.db?cache=shared"), &gorm.Config{
	Logger: logger.Default.LogMode(logger.Info),
})

func initDb() {
	_ = db.AutoMigrate(&Post{})
	for i := 1; i <= 100; i++ {
		p := Post{
			Number: i,
		}
		db.Save(&p)
	}
}
func simple() {
	q := db.Model(Post{})
	var dest []Post
	var adapt = adapter.NewGORMAdapter(q)
	paginator := paginator.SimplePaginator(adapt, 10, 1)
	_ = paginator.Get(&dest)
	//获取最后页码
	page, err := paginator.GetLastPage()
	fmt.Println(fmt.Sprintf("获取最后页码:%v", page))
	fmt.Println(fmt.Sprintf("获取最后页码错误信息:%v", err))
	//获取总数
	total, err := paginator.GetTotal()
	fmt.Println(fmt.Sprintf("获取总数:%v", total))
	fmt.Println(fmt.Sprintf("获取总数错误信息:%v", err))
	fmt.Println(fmt.Sprintf("当前页码:%v", paginator.GetCurrentPage()))
	fmt.Println(fmt.Sprintf("每页显示多少条数:%v", paginator.GetListRows()))
	fmt.Println(fmt.Sprintf("是否还可以进行分页:%v", paginator.HasPages()))
	fmt.Println(dest)
}

func Total() {
	q := db.Model(Post{}).Where([]int64{20, 21, 22}).Order("id desc")
	var dest []Post
	var adapt = adapter.NewGORMAdapter(q)
	t, _ := adapt.Length()
	paginator := paginator.TotalPaginator(adapt, 10, 1, t)
	//_ = paginator.Get(&dest)
	r := paginator.GetResult(&dest)
	marshal, _ := json.Marshal(r)
	xmlbytes, err := xml.Marshal(r)
	fmt.Println(string(marshal))
	fmt.Println(string(xmlbytes))
	//获取最后页码
	page, err := paginator.GetLastPage()
	fmt.Println(fmt.Sprintf("获取最后页码:%v", page))
	fmt.Println(fmt.Sprintf("获取最后页码错误信息:%v", err))
	//获取总数
	total, err := paginator.GetTotal()
	fmt.Println(fmt.Sprintf("获取总数:%v", total))
	fmt.Println(fmt.Sprintf("获取总数错误信息:%v", err))
	fmt.Println(fmt.Sprintf("当前页码:%v", paginator.GetCurrentPage()))
	fmt.Println(fmt.Sprintf("每页显示多少条数:%v", paginator.GetListRows()))
	fmt.Println(fmt.Sprintf("是否还可以进行分页:%v", paginator.HasPages()))
	fmt.Println(dest)
}

func main() {
	Total()
}

Adapters

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

// Length 数据长度
Length() (int64, error)
// Slice 切割数据
Slice(offset, length int64, dest any) error

GORM Adapter

q := db.Model(Post{}).Where("published_at > ?", time.Now())
var adapt = adapter.NewGORMAdapter(q)
paginator := paginator.SimplePaginator(adapt, 10, 1)

Slice adapter

var source = []int{
	1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
	11, 22, 33, 44, 55, 66, 77, 88, 99, 199,
	111, 222, 333, 444, 555, 666, 777, 888, 999, 199,
	1111, 2222,
}
var adapt = adapter.NewSliceAdapter(source)
paginator := paginator.SimplePaginator(adapt, 10, 5)
var dest []int
paginator.Get(&dest)

加入我们

如果你认可我们的开源项目,有兴趣为 go-paginator 的发展做贡献,竭诚欢迎加入我们一起开发完善。无论是报告错误或是 Pull Request 开发,那怕是修改一个错别字也是对我们莫大的帮助。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IAdapter

type IAdapter interface {
	// Length 数据长度
	Length() (int64, error)
	// Slice 切割数据(分页)
	Slice(offset, length int64, dest any) error
}

type IPaginator

type IPaginator interface {
	Clear()
	// Get 获取数据
	Get(dest any) error
	// SetDestItems 设置输出源
	SetDestItems(dest any)
	// GetDestItems 获取输出源
	GetDestItems(dest any) any
	// GetResult 获取 Result结构体数据
	GetResult(dest any) *Result
	// SetCurrentPage 设置当前页数
	SetCurrentPage(currentPage int64)
	// GetCurrentPage 获取当前页页码
	GetCurrentPage() int64
	// GetTotal 获取数据总条数
	GetTotal() (int64, error)
	// GetListRows 获取每页数量
	GetListRows() int64
	//GetLastPage 获取最后一页页码
	GetLastPage() (int64, error)
	//HasPages 数据是否足够分页
	HasPages() bool
}

func Make

func Make(adapter IAdapter, listRows int64, currentPage int64, total int64, simple bool, options Options) IPaginator

func SimplePaginator

func SimplePaginator(adapter IAdapter, listRows, currentPage int64) IPaginator

func TotalPaginator

func TotalPaginator(adapter IAdapter, listRows, currentPage, total int64) IPaginator

type Options

type Options struct {
	VarPage  string
	Path     string
	Query    map[string]string
	Fragment string
}

func DefaultOptions

func DefaultOptions() Options

type Paginator

type Paginator struct {
	Simple bool
	//数据集适配器
	IAdapter IAdapter
	//设置输出源
	DestItems any
	//当前页
	CurrentPage int64
	//最后一页
	LastPage int64
	//数据总数
	Total int64
	//每页数量
	ListRows int64
	//是否有下一页
	HasMore bool
	//分页配置
	Options Options
}

func (Paginator) Clear

func (p Paginator) Clear()

func (*Paginator) Get

func (p *Paginator) Get(dest any) error

Get 获取指定长度的数据

func (*Paginator) GetCurrentPage

func (p *Paginator) GetCurrentPage() int64

GetCurrentPage 获取当前页页码

func (*Paginator) GetDestItems

func (p *Paginator) GetDestItems(dest any) any

func (*Paginator) GetLastPage

func (p *Paginator) GetLastPage() (int64, error)

GetLastPage 获取最后一页页码

func (*Paginator) GetListRows

func (p *Paginator) GetListRows() int64

GetListRows 获取每页数量

func (*Paginator) GetResult

func (p *Paginator) GetResult(dest any) *Result

GetResult 获取 Result结构体数据

func (*Paginator) GetTotal

func (p *Paginator) GetTotal() (int64, error)

GetTotal 获取数据总条数

func (*Paginator) HasPages

func (p *Paginator) HasPages() bool

HasPages 数据是否足够分页

func (*Paginator) SetCurrentPage

func (p *Paginator) SetCurrentPage(currentPage int64)

SetCurrentPage 设置当前页数

func (*Paginator) SetDestItems

func (p *Paginator) SetDestItems(dest any)

type Result

type Result struct {
	Total       int64 `json:"total" xml:"total"`
	Data        any   `json:"data" xml:"data"`
	PerPage     int64 `json:"per_page" xml:"perPage"`
	CurrentPage int64 `json:"current_page" xml:"currentPage"`
	LastPage    int64 `json:"last_page" xml:"lastPage"`
}

Directories

Path Synopsis
_example

Jump to

Keyboard shortcuts

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