您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)使用thinkphp5框架怎么在前后端分離項(xiàng)目中實(shí)現(xiàn)分頁功能,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
方法一
利用tp5提供的paginate方法實(shí)現(xiàn)自動(dòng)分頁
參數(shù)
page第幾頁,paginate分頁方法會(huì)自動(dòng)獲取
size 每頁數(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)分頁功能,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。