开发者

go语言制作分页器实例探究

目录
  • Install
  • Usage
  • Get & Render method
  • Adapters
    • GORM Adapter
    • XORM Adapter
    • Slice adapter

Install

$ 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 destICxaPhgundex []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.Spr编程客栈intf("当前页码:%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

GetRenderdata
Execute mandatory&data datanil&data
Execute mandatory&data dataTransfer dataTransfer data
Do not executenilnull
Do not executeTransfer dataGetjs 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)

以上就是go语言制作分页器实例探究的详细内容,更多关于go语言实现分页器的资料请关注编程客栈(www.devze.com)其它相关文章!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜