如何使用Laravel在LengthAwarePaginator中添加下拉页面选择

时间:2017-10-07 19:46:53

标签: php laravel pagination

我正在使用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

请在此脚本中帮助您选择下拉列表。

提前致谢!

2 个答案:

答案 0 :(得分:1)

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

<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);
}

答案 1 :(得分: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传递回视图,以便可以用来确保选择了当前长度