tp5.1分頁接口


分析接口

  • 請求方式:查詢,肯定是Get
  • 請求路徑:分頁查詢,/api/item/brand/page
  • 請求參數:根據我們剛才編寫的頁面,有分頁功能,有排序功能,有搜索過濾功能,因此至少要有5個參數:

    • page:當前頁,int
    • rows:每頁大小,int
    • sortBy:排序字段,String
    • desc:是否為降序,boolean
    • key:搜索關鍵詞,String
    http://local.leyou.com/api/item/brand/page?page=1&rows=5&sortBy=id&desc=true
  • 響應結果:分頁結果一般至少需要兩個數據

    • total:總條數
    • items:當前頁數據
    • totalPage:有些還需要總頁數
<?php

namespace app\api\controller;

use app\api\model\Brands;
use think\Controller;
use think\Request;

class Brand extends Controller
{
    //分頁方法
    public function page(Request $request){
        //dump($request->param());
        //當前頁
        $page = $request->param('page',1);
        //顯示多少條數據
        $rows = $request->param('rows',5);
        //排序字段
        $sort = $request->param('sortBy','id');
        //排序方式,傳的值是布爾desc=true,默認從小到大
        $desc = $request->param('desc','asc');
        //為true則倒序,false順序
        $desc = ($desc==true) ? 'desc':'asc';
        $key = $request->param('key','');
        //調用模型獲取數據
        $items = Brands::whereLike('letter',"%{$key}%")
            ->page($page,$rows)
            ->order($sort,$desc)
            ->select();
        //1.總記錄數
        $total  = Brands::count();
        //dump($total);
        //2.總頁碼
        $totalPage = ceil($total/$rows);
        $data = [
            'total' =>$total,
            'items' => $items,
            'totalPage' => $totalPage
        ];
        return json($data);
    }
}

 

效果
1.PNG

 最后修改:2019 年 12 月 15 日 10 : 36 PM


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM