2017-10-07 174 views
1

我正在使用Laravel分页脚本。我想在LengthAwarePaginator中添加每页下拉选择,这样我可以根据脚本中的页面选择列出所有条目。 我的脚本:如何使用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

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

在此先感谢!

回答

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

但分页底页上仍存在 所有记录没有显示 http://prntscr.com/gut3r5 –

+0

好的,实际上你不必切片你的数组,只要确保它是一个集合,所以你可以分页。看看我编辑的答案是否有帮助。 –

+0

谢谢你的回答有帮助。问题解决了。 –

相关问题