温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用thinkphp5框架怎么在前后端分离项目中实现分页功能

发布时间:2021-05-27 16:50:37 来源:亿速云 阅读:218 作者:Leah 栏目:开发技术

本篇文章给大家分享的是有关使用thinkphp5框架怎么在前后端分离项目中实现分页功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

方法一

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

参数

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

size  每页数量

代码

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"
}

以上就是使用thinkphp5框架怎么在前后端分离项目中实现分页功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI