【问题标题】:How to add dropdown page selection in LengthAwarePaginator using Laravel如何使用 Laravel 在 LengthAwarePaginator 中添加下拉页面选择
【发布时间】:2019-09-24 11:04:16
【问题描述】:

我正在使用脚本进行 Laravel 分页。我想在 LengthAwarePaginator 中添加每页下拉选择,以便我可以根据脚本中的页面选择列出所有条目。 我的脚本:

   public function index(Request $request){

    $data = [];
    $keyword = request('keyword','');
    $applicationData = $this->ppObjAdmin->getBackOfficeApplications(['keyword' =>$keyword]);
    $paginate =10;
    $pageStart = request('page', 1);
    $offSet    = ($pageStart * $paginate) - $paginate;
    $itemsForCurrentPage = array_slice($applicationData,$offSet, $paginate);
    $journeyListData = new LengthAwarePaginator($itemsForCurrentPage , count($applicationData) , $paginate , Paginator::resolveCurrentPage() , ['path' => Paginator::resolveCurrentPath()]);
    $data['journeyData'] = $journeyListData;
    $data['searchKeyword'] = $keyword;

    return view('admin.listing',$data);
}

下拉html:

                                <select class="page_limit pgination-select" onchange="page_limit(this)">
                                    <option value="10" >10 Entries per page</option>
                                    <option value="20 " >20 Entries per page</option>
                                    <option value="30 " >30 Entries per page</option>
                                    <option value="50">50 Entries per page</option>
                                </select>
                            </div>`enter code here`

请看附件链接问题显示更清楚。 http://prntscr.com/guto00

请在此脚本中提供帮助以进行下拉选择。

提前致谢!

【问题讨论】:

    标签: php laravel pagination


    【解决方案1】:

    对我来说更优雅的形式是这种方式(http://novate.co.uk/allow-user-to-choose-pagination-length-via-dropdown-laravel/):

    在显示链接时,我们需要追加当前页面长度

    {{ $members->appends(compact('items'))->links() }}

    选择列表创建如下

    > <form>
    >     <select id="pagination">
    >         <option value="5" @if($items == 5) selected @endif >5</option>
    >         <option value="10" @if($items == 10) selected @endif >10</option>
    >         <option value="25" @if($items == 25) selected @endif >25</option>
    >     </select> </form>
    > 
    > <script>
    >     document.getElementById('pagination').onchange = function() { 
    >         window.location = "{!! $members->url(1) !!}&items=" + this.value; 
    >     };  </script>
    

    更改下拉列表会导致页面重新加载,将items=10 等传递给控制器​​。控制器检查项目或使用默认值。

    public function index(Request $request)
    {
        $items = $request->items ?? 10;      // get the pagination number or a default
    
        $club = Club::findOrFail(session('club'));
    
        $members = $club->members()->paginate($items);
    
        return view('club.' . config('app.locale') . '.index')
            ->withClub($club)
            ->withMembers($members)
            ->withItems($items);
    }
    

    在控制器方法中,$itemsis 传回给视图,以便可以用来确保当前长度被选中

    【讨论】:

      【解决方案2】:

      您可以将您的选择放入一个表单中,如下所示:

      <form method="GET">
          <input type="hidden" name="_token" value="{{ csrf_token() }}">
          <select class="page_limit pgination-select" name="limit">
              <option value="10">10 Entries per page</option>
              <option value="20">20 Entries per page</option>
              <option value="30">30 Entries per page</option>
              <option value="50">50 Entries per page</option>
          </select>
          <button type="submit">Send</button>
      </form>
      

      然后您只需在控制器中检查请求中的参数:

      public function index(Request $request){
          $data = [];
          $keyword = request('keyword','');
          $applicationData = $this->ppObjAdmin->getBackOfficeApplications(['keyword' =>$keyword]);
          $page_limit = $request->limit ?: 10;
          $currentPage = Paginator::resolveCurrentPage() ?: 1;
      
          // applicationData has to be an instance of the class Collection
          $items = $applicationData instanceof Collection ? $applicationData : Collection::make($applicationData);
          $itemsForCurrentPage = $items->forPage($currentPage, $page_limit);
      
          $journeyListData = new LengthAwarePaginator($itemsForCurrentPage, $items->count(), $page_limit , $currentPage, ['path' => Paginator::resolveCurrentPath()]);
      
          $data['journeyData'] = $journeyListData;
          $data['searchKeyword'] = $keyword;
      
          return view('admin.listing',$data);
      }
      

      【讨论】:

      • 但是底部页面的分页仍然存在所有记录都没有显示prntscr.com/gut3r5
      • 好的,实际上你不必对数组进行切片,只要确保它是一个集合,就可以对其进行分页。看看我编辑的答案是否有帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-23
      • 2021-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      相关资源
      最近更新 更多