zoukankan      html  css  js  c++  java
  • laravel手动数组分页

    laravel文档中已经有写如何自己使用分页类去分页了,但没有详细说明。

    如果你想手动创建分页实例并且最终得到一个数组类型的结果,可以根据需求来创建 IlluminatePaginationPaginator 或者 IlluminatePaginationLengthAwarePaginator 实例来实现。

    具体可以看IlluminatePaginationLengthAwarePaginator中的这段代码:

    public function __construct($items, $total, $perPage, $currentPage = null, array $options = [])
     {
      foreach ($options as $key => $value) {
       $this->{$key} = $value;
      }
     
      $this->total = $total;
      $this->perPage = $perPage;
      $this->lastPage = max((int) ceil($total / $perPage), 1);
      $this->path = $this->path !== '/' ? rtrim($this->path, '/') : $this->path;
      $this->currentPage = $this->setCurrentPage($currentPage, $this->pageName);
      $this->items = $items instanceof Collection ? $items : Collection::make($items);
     }

    以下为具体实现代码:

    public function index(Request $request)
     {
      //数据A
      $dataA = User::where('status', 1)->get()->toArray();
      //数据B
      $dataB = User::where('status', 2)->get()->toArray();
      $data = array_merge($dataA, $dataB);
      //当前页数 默认1
      $page = $request->page ?: 1;
      //每页的条数
      $perPage = 4;
      //计算每页分页的初始位置
      $offset = ($page * $perPage) - $perPage;
       //实例化LengthAwarePaginator类,并传入对应的参数
      $data = new LengthAwarePaginator(array_slice($data, $offset, $perPage, true), count($data), $perPage,
       $page, ['path' => $request->url(), 'query' => $request->query()]);
      return view('admin.users.index', compact('data'));
     }
     
    //视图中
    {{ $data->links() }}
  • 相关阅读:
    glob
    【转载】分词小结
    【pandas】基本处理
    【pandas】函数映射:map() /apply() /applymap()
    C# 获取计算机信息
    oracle 关键字
    oracle 触发器
    C# DataTable分页处理
    C#常用控件介绍
    C# 关键字【转】
  • 原文地址:https://www.cnblogs.com/niuben/p/12378265.html
Copyright © 2011-2022 走看看