Laravel 分页和排序表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47945661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel pagination and sort table
提问by Msaas
I'm using sort function and pagination but the problem when I use sort for any column works fine but when I click to the next page using pagination the sort change and have to click on the column again for sorting. so how to keep sorting while using pagination?
我正在使用排序功能和分页,但是当我对任何列使用排序时出现问题,但是当我使用分页单击到下一页时,排序会发生变化,并且必须再次单击该列进行排序。那么如何在使用分页时保持排序?
<tr>
<th>
<div class="checkbox checkbox-replace">
<input type="checkbox" id="chk-3">
</div>
</th>
<th>@sortablelink('details','Details')</th>
<th>@sortablelink('postingdate','Date')</th>
<th>@sortablelink('description','Description')</th>
<th>@sortablelink('amount','Amount')</th>
<th>@sortablelink('type','Type')</th>
<th>@sortablelink('slip','Slip#')</th>
<th>@sortablelink('vendor_id','Vendor')</th>
<th>@sortablelink('category_id','Category')</th>
</tr>
public function index()
{
$checks = Checks::orderBy('id', 'asc')->get();
$checks= Checks::sortable()->paginate(5);
return view('home',compact('checks'));
}
回答by Mathieu Ferre
You can append query string to your link by doing this :
您可以通过执行以下操作将查询字符串附加到您的链接:
{{ $users->appends(['sort' => 'votes'])->links() }}
replace sort
by your sortable query, and votes
by something like request()->sort
替换sort
为您的可排序查询,以及votes
类似的内容request()->sort
回答by Qrazier
According to this page, you also can do your pagination like this:
根据此页面,您还可以像这样进行分页:
//Controller
public function index()
{
$checks = Checks::orderBy('id')->paginate(5);
$links = $checks ->appends(['sort' => 'id'])->links();
return view('home', compact('checks', 'links'));
}
And in your view, you can just call your pagination links like this
在您看来,您可以像这样调用分页链接
{{ $links }}
Hope it helps. Cheers!
希望能帮助到你。干杯!
回答by Chrisphine
{{links()}}
will still give a messed up sort when you paginate. This is the same as {!! link() !!}
also and {{ $checks->links()}}
Rather, use the code below. It will correct the error.
{{links()}}
分页时仍然会出现乱七八糟的排序。这与{!! link() !!}
也相同,{{ $checks->links()}}
而是使用下面的代码。它将纠正错误。
//for the Controller
public function index()
{
$checks = Checks::sortable()->paginate(5);
return view('home', compact('checks'));
}
//for the blade template in laravel append this in div after the table
{!! $checks->appends(\Request::except('page'))->render() !!}][1]