go语言如何做分页器呢?

目录
文章目录隐藏
  1. 安装
  2. Usage
  3. Get & Render method
  4. Adapters

go 语言如何做分页器呢?

安装

$ go get github.com/pkg6/go-paginate

Usage

package main

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

type Post struct {
    ID     uint `gormp:"primarykey" json:"id"`
    Number int  `json:"number"`
}
type PostIndex struct {
    ID     uint `gormp:"primarykey" json:"id"`
    Number int  `json:"number"`
    Index  int  `json:"index"`
}

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

func init() {
    _ = 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 = gormp.Adapter(q)
    myPage := paginate.SimplePaginate(adapt, 10, 1)
    _ = myPage.Get(&dest)
    //获取最后页码
    page, err := myPage.GetLastPage()
    fmt.Println(fmt.Sprintf("获取最后页码:%v", page))
    fmt.Println(fmt.Sprintf("获取最后页码错误信息:%v", err))
    //获取总数
    total, err := myPage.GetTotal()
    fmt.Println(fmt.Sprintf("获取总数:%v", total))
    fmt.Println(fmt.Sprintf("获取总数错误信息:%v", err))
    fmt.Println(fmt.Sprintf("当前页码:%v", myPage.GetCurrentPage()))
    fmt.Println(fmt.Sprintf("每页显示多少条数:%v", myPage.GetListRows()))
    fmt.Println(fmt.Sprintf("是否还可以进行分页:%v", myPage.HasPages()))
    fmt.Println(dest)
}

func Total() {
    q := db.Model(Post{}).Where([]int64{20, 21, 22}).Order("id desc")
    var dest []Post
    var adapt = gormp.Adapter(q)
    t, _ := adapt.Length()
    myPage := paginate.TotalPaginate(adapt, 10, 1, t)
    _ = myPage.Get(&dest)
    var destIndex []PostIndex
    for i, post := range dest {
        destIndex = append(destIndex, PostIndex{ID: post.ID, Number: post.Number, Index: i})
    }
    // 先执行当 get 方法此时为 nil data 数据为 dest
    // 不执行 get 方法 此时传入 dest,data 就是 dest
    // 执行 get 方法 此时在传入新的结构体 data 就是 data
    // 不执行 get 此时传入 nil 就返回 nil
    r := myPage.Render(nil)
    jsonBytes, _ := json.Marshal(r)
    xmlBytes, err := xml.Marshal(r)
    fmt.Println(string(jsonBytes))
    fmt.Println(string(xmlBytes))
    //获取最后页码
    page, err := myPage.GetLastPage()
    fmt.Println(fmt.Sprintf("获取最后页码:%v", page))
    fmt.Println(fmt.Sprintf("获取最后页码错误信息:%v", err))
    //获取总数
    total, err := myPage.GetTotal()
    fmt.Println(fmt.Sprintf("获取总数:%v", total))
    fmt.Println(fmt.Sprintf("获取总数错误信息:%v", err))
    fmt.Println(fmt.Sprintf("当前页码:%v", myPage.GetCurrentPage()))
    fmt.Println(fmt.Sprintf("每页显示多少条数:%v", myPage.GetListRows()))
    fmt.Println(fmt.Sprintf("是否还可以进行分页:%v", myPage.HasPages()))
    fmt.Println(dest)
}

func main() {
    Total()
    simple()
}

Get & Render method

Get Render data
Execute mandatory&data data nil &data
Execute mandatory&data data Transfer data Transfer data
Do not execute nil null
Do not execute Transfer data Get method executed

Adapters

适配器必须实现具有两种方法的“adapter”接口:

Length() (int64, error)
Slice(offset, length int64, dest any) error

GORM Adapter

var db, _ = gorm.Open(sqlite.Open("gorm.db?cache=shared"), &gorm.Config{
    Logger: logger.Default.LogMode(logger.Info),
})
q := db.Model(Post{}).Where([]int64{20, 21, 22}).Order("id desc")
var dest []Post
var adapt = gromp.Adapter(q)
t, _ := adapt.Length()
myPage := paginate.TotalPaginate(adapt, 10, 1, t)

XORM Adapter

var engine, _ = xorm.NewEngine("sqlite3", "xorm.db")
session := engine.Table(Post{})
var dest []Post
var adapt = xormp.Adapter(session)
total, _ := adapt.Length()
myPage := paginate.TotalPaginate(adapt, 10, 1, total)

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 = slicep.Adapter(source)
myPage := paginate.SimplePaginate(adapt, 10, 5)

「点点赞赏,手留余香」

1

给作者打赏,鼓励TA抓紧创作!

微信微信 支付宝支付宝

还没有人赞赏,快来当第一个赞赏的人吧!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系maynote@foxmail.com处理
码云笔记 » go语言如何做分页器呢?

发表回复