thinkphp5框架前后端分离项目实现分页功能的方法分析

2022-04-15 0 886

本文实例讲述了thinkphp5框架前后端分离项目实现分页功能的方法。分享给大家供大家参考,具体如下:

方法一

利用tp5提供的paginate方法实现自动分页

参数

page第几页,paginate分页方法会自动获取

size  每页数量

代码

/**
* Notes:消费记录
* Date: 2019/6/25
* Time: 15:43
* @param Request $request
* @return \think\response\Json
*/
public function getMyConsumeLog(Request $request)
{
    global $_W;
    $size = $request->param('size', 6);
    $list = $this->model->getListByMid($_W['user']['id'],$size);
    return json(['data' => $list, 'error' => 0, 'message' => 'success']);
}
public function getListByMid($mid,$size = 10){
    $res = $this
      ->alias('c')
      ->field('c.*,b.book_name,b.book_flash,s.section_title')
      ->leftJoin('booksection s','c.chapter_id = s.id')
      ->leftJoin('book b','s.book_id = b.id')
      ->where('c.mid',$mid)
      ->order('c.id desc')
      ->paginate($size);
    return $res;
}

返回数据

{
    “data”: {
        “total”: 1,
        “per_page”: 1,
        “current_page”: 1,
        “last_page”: 1,
        “data”: [
            {
                “id”: 105,
                “mid”: 55,
                “book_id”: 31,
                “chapter_id”: 46046,
                “score”: 27,
                “create_time”: 1561447448,
                “book_name”: “桃运村支书”,
                “book_flash”: “https://cdnxiaoshuo.t.com/FiO6TM0N4kpzKB7tqrDko64ZS4H4”,
                “section_title”: “第29章 康庄大道”
            }
        ]
    },
    “error”: 0,
    “message”: “success”
}

方法二

利用limit方法

$curr_page = $request->param('page', 1);
    $size = $request->param('size', 6);
$list = $consume_model->getListByWhere($curr_page, $size, $where);
    $num = $consume_model->getListByWhereCount($where);
    return json(['data' => $list,'num' => $num,'error' => 0, 'message' => 'success']);
public function getListByWhere($curr_page,$limit = 10,$where = null){
    $res = $this
      ->alias('c')
      ->field('c.*,b.book_name,s.section_title')
      ->leftJoin('booksection s','c.chapter_id = s.id')
      ->leftJoin('book b','s.book_id = b.id')
      ->where($where)
      ->order('c.id desc')
      ->limit($limit*($curr_page - 1),$limit)
      ->select()
      ->toArray();
    return $res;
}
public function getListByWhereCount($where = null){
    $count = $this
      ->alias('c')
      ->where($where)
      ->count();
    return $count;
}

返回值

{
    “data”: [
        {
            “id”: 2,
            “mid”: 4,
            “book_id”: 4,
            “chapter_id”: 22,
            “score”: 30,
            “create_time”: 0,
            “book_name”: “复仇者联盟I”,
            “section_title”: “第11章  你是睡”
        },
        {
            “id”: 1,
            “mid”: 4,
            “book_id”: 29,
            “chapter_id”: 34,
            “score”: 20,
            “create_time”: 1598999,
            “book_name”: “复仇者联盟II”,
            “section_title”: “第11章  你是睡”
        }
    ],
    “num”: 2,
    “total_coin”: 50,
    “error”: 0,
    “message”: “success”
}

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题

免责声明:
1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

NICE源码网 PHP编程 thinkphp5框架前后端分离项目实现分页功能的方法分析 https://www.niceym.com/12856.html