本篇文章給大家分享的是有關(guān)使用thinkphp5框架怎么在前后端分離項(xiàng)目中實(shí)現(xiàn)分頁(yè)功能,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
創(chuàng)新互聯(lián)建站專注于富寧企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城建設(shè)。富寧網(wǎng)站建設(shè)公司,為富寧等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)方法一
利用tp5提供的paginate方法實(shí)現(xiàn)自動(dòng)分頁(yè)
參數(shù)
page第幾頁(yè),paginate分頁(yè)方法會(huì)自動(dòng)獲取
size 每頁(yè)數(shù)量
代碼
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; }
返回?cái)?shù)據(jù)
{
"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": "桃運(yùn)村支書",
"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": "復(fù)仇者聯(lián)盟I",
"section_title": "第11章 你是睡"
},
{
"id": 1,
"mid": 4,
"book_id": 29,
"chapter_id": 34,
"score": 20,
"create_time": 1598999,
"book_name": "復(fù)仇者聯(lián)盟II",
"section_title": "第11章 你是睡"
}
],
"num": 2,
"total_coin": 50,
"error": 0,
"message": "success"
}
以上就是使用thinkphp5框架怎么在前后端分離項(xiàng)目中實(shí)現(xiàn)分頁(yè)功能,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。