分页实现

输入与输出

前端:antd-pro,后端:go
查看列表页-查询表格的url,发现模拟数据的url及响应如下:

输入:
api?currentPage=2&pageSize=10
输出:
{
    "list": [
        {
            "key": 0,
            "title": "一个任务名称 0",
            "owner": "曲丽丽",
            "description": "这是一段描述",
            "updatedAt": "2017-06-30T16:00:00.000Z",
            "createdAt": "2017-06-30T16:00:00.000Z",
        },
    "pagination": {
        "total": 46,
        "pageSize": 10,
        "current": 2
    }
}
效果
image.png
代码
package helper

import (
    "errors"
    "math"
)

type Pagination struct {
    Current   int64   //当前页
    Pagesize  int64   //每页条数
    Total     int64   //总条数
    PageCount int64   //总页数
    Nums      []int64 //分页序数
    NumsCount int64   //总页序数
}

/*
arg1:当前页
arg2:显示条数
arg3:总条数
return1:*Pagination
return2:error

示例:

func (this *TestController) Paging() {
    current, _ := this.GetInt64("current")
    pageSize, _ := this.GetInt64("pageSize")
    this.Data["pagination"] = Paginator(current, pageSize, 365)
    this.serverJson()
}

*/
func Paginator(current, pagesize, total int64) (*Pagination, error) {
    if total < 0 {
        return nil, errors.New("Error: invalid total num, total must >=0 ")
    }

    if current < 1 {
        current = 1
    }
    if pagesize < 1 {
        pagesize = 10
    }

    page_count := math.Ceil(float64(total) / float64(pagesize))

    pagination := new(Pagination)
    pagination.Current = current
    pagination.Pagesize = pagesize
    pagination.Total = total
    pagination.PageCount = int64(page_count)
    pagination.NumsCount = 7
    pagination.setNums()

    return pagination, nil
}

func (this *Pagination) setNums() {
    this.Nums = []int64{}
    if this.PageCount == 0 {
        return
    }

    half := math.Floor(float64(this.NumsCount) / float64(2))
    begin := this.Current - int64(half)
    if begin < 1 {
        begin = 1
    }

    end := begin + this.NumsCount - 1
    if end >= this.PageCount {
        begin = this.PageCount - this.NumsCount + 1
        if begin < 1 {
            begin = 1
        }
        end = this.PageCount
    }

    for i := begin; i <= end; i++ {
        this.Nums = append(this.Nums, i)
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 前言 昨天在博客园的博问上帮一位园友解决了一个问题,我觉得有必要记录一下,万一有人也遇上了呢。 问题描述 园友是做...
    seay阅读 23,576评论 23 39
  • 我们可以非常容易地使用 CBV 来实现分页功能。但首先我想手工分页,这样就更有助于我们理解背后的机制,这样它就不那...
    冰321阅读 5,789评论 1 23
  • ABP入门系列目录——学习Abp框架之实操演练源码路径:Github-LearningMpaAbp 完成了任务清单...
    圣杰阅读 17,054评论 6 9
  • 原生分页的思路(原理):首先和产品经理定一下每页显示多少条目, 然后通过后台的某一个数据接口,获取某一页的数据(需...
    谢聃阅读 4,381评论 0 0
  • “明月出天山,苍茫云海间。长风几万里,吹度玉门关。” 那年中秋既望的下午,我来到武当山,住进了道教功夫学院,学院是...
    无无行阅读 5,935评论 0 5

友情链接更多精彩内容