zoukankan      html  css  js  c++  java
  • [Go]GO语言实战-开源WEB客服GO-FLY-gorm下分页的实现

    分页功能几乎是每个项目里都会使用的功能,在使用gorm的前提下,下面这样实现分页.

    前端使用的是elementui , 只需要返回两个参数就可以前端分页了 , 总页数和每页的条数

    后端需要知道两个参数, 当前第几页和每页的条数

    比如下面的代码:

    里面的page是前端传过来的 , pagesize是配置里规定的, 就可以交给gorm去分页了

    func GetVisitors(c *gin.Context) {
        page,_:=strconv.Atoi(c.Query("page"))
        kefuId,_:=c.Get("kefu_name")
        vistors:=models.FindVisitorsByKefuId(uint(page),config.VisitorPageSize,kefuId.(string))
        count:=models.CountVisitorsByKefuId(kefuId.(string))
        c.JSON(200, gin.H{
            "code": 200,
            "msg":  "ok",
            "result":gin.H{
                "list":vistors,
                "count":count,
                "pagesize":config.PageSize,
            },
        })
    }

    gorm里面的代码:

    主要是offset 和 limit的使用

    func FindVisitorsByKefuId(page uint,pagesize uint,kefuId string)[]Visitor{
        offset:=(page-1)*pagesize
        if offset<0{
            offset=0
        }
        var visitors []Visitor
        DB.Where("to_id=?",kefuId).Offset(offset).Limit(pagesize).Order("status desc, updated_at desc").Find(&visitors)
        return visitors
    }

    效果是这样的

     代码就在下面的github里面 

  • 相关阅读:
    asp.net HttpModule和HttpHandler
    Asp.Net生命周期和Http管道技术
    降低web服务器压力
    html里嵌入CSS的三种方式
    php实现简单视图模板(视图引擎)
    ASP.NET MVC路由配置
    igel udc2 config
    单IP、网络、别名管道限速的设置
    Apple SIP简介及在Clover中如何控制
    Hackintosh
  • 原文地址:https://www.cnblogs.com/taoshihan/p/13650886.html
Copyright © 2011-2022 走看看