Laravel (3) 分页排序与过滤
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15215210/
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 (3) Pagination Sorting & Filtering
提问by MCG
I have a list of all the "servers" in my "servers" table returned in my view with pagination. I have been struggling to figure out how to get sorting (asc & desc if possible) and filtering (searching within results) working.
我有一个“服务器”表中所有“服务器”的列表,在我的视图中以分页方式返回。我一直在努力弄清楚如何进行排序(如果可能的话,升序和降序)和过滤(在结果中搜索)工作。
Here is my controller code:
这是我的控制器代码:
$servers = Server::paginate(5);
return View::make('servers.list')
->with('game', '')
->with('servers', $servers);
Here is my view code for the sorting:
这是我的排序视图代码:
<ul class="nav">
<li class="active"><a href="#"><i class="icon-angle-down"></i>{{ Lang::line('servers.rank')->get() }}</a></li>
<li><a href="#">{{ Lang::line('servers.date')->get() }}</a></li>
<li><a href="#">{{ Lang::line('servers.language')->get() }}</a></li>
<li><a href="#">{{ Lang::line('servers.uptime')->get() }}</a></li>
<li>{{ HTML::link(URL::full() .'?sort=votes', Lang::line('servers.votes')->get()) }} </li>
</ul>
I would like for the sorting to be done via simple anchor links and clicking on Votes or Rank or Date will return data with that sorting. Clicking on the same sorting anchor which is currently selected will reverse the direction of the sort.
我希望通过简单的锚链接完成排序,点击投票或排名或日期将返回具有该排序的数据。单击当前选定的同一排序锚点将反转排序方向。
I also have a bunch of "filter" options such as categories and integer ranges which when applied would "filter"/search the table and return the results with the same sorting as selected before the filter.
我还有一堆“过滤器”选项,例如类别和整数范围,当应用它们时将“过滤”/搜索表格并返回与过滤器之前选择的排序相同的结果。
Is all/any of this possible with the pagination class? If not what might be the way I go about this? Not really sure the best way when using laravel.
分页类是否可以实现所有/任何这些?如果不是,我会怎么做呢?不确定使用 Laravel 时的最佳方法。
回答by dynamo
This is easy to do in laravel thanks to the great pagination and input class. Try this in your views:
由于出色的分页和输入类,这在 Laravel 中很容易做到。在你的观点中试试这个:
Example View
示例视图
Cleaned up view
清理视图
<form action="" method="get" id="filter">
Show <select name="game_id">
<option value="">All</option>
<?php foreach ($games as $game):?>
<option value="<?=$game->id?>" <?=($game->id == Input::get('game_id')) ? 'selected="selected"' : null?>><?=$game->name?></option>
<?php endforeach;?>
</select>
Show <select name="server_id">
<option value="">All</option>
<?php foreach ($servers as $server):?>
<option value="<?=$server->id?>" <?=($server->id == Input::get('server_id')) ? 'selected="selected"' : null?>><?=$server->name?></option>
<?php endforeach;?>
</select>
<input type="submit" value="Filter" rel="filter">
</form>
<hr>
<?php if (count($servers) > 0):?>
<?=$pagination?>
<table>
<tr>
<th><a href="<?=URL::to('servers?sort=id'.$querystr)?>">ID</a></th>
<th><a href="<?=URL::to('servers?sort=rank'.$querystr)?>">RANK</a></th>
<th><a href="<?=URL::to('servers?sort=date'.$querystr)?>">DATE</a></th>
<th><a href="<?=URL::to('servers?sort=language'.$querystr)?>">LANGUAGE</a></th>
<th><a href="<?=URL::to('servers?sort=uptime'.$querystr)?>">UP TIME</a></th>
<th><a href="<?=URL::to('servers?sort=votes'.$querystr)?>">VOTES</a></th>
</tr>
<tr>
<td>
...
</td>
</tr>
</table>
<?=$pagination?>
<?php else:?>
<h2>No results found.</h2>
<?php endif;?>
Example Controller
示例控制器
public function get_action()
{
// CACHE SORTING INPUTS
$allowed = array('rank', 'date', 'language', 'uptime', 'votes'); // add allowable columns to search on
$sort = in_array(Input::get('sort'), $allowed) ? Input::get('sort') : 'id'; // if user type in the url a column that doesnt exist app will default to id
$order = Input::get('order') === 'asc' ? 'asc' : 'desc'; // default desc
$servers = Server::order_by($sort, $order);
// FILTERS
$game = null;
$server = null;
if (Input::has('game_id')) {
$servers = $servers->where('game_id', Input::get('game_id'));
$game = '&game_id='.Input::get('game_id');
}
if (Input::has('server_id')) {
$servers = $servers->where('server_id', Input::get('server_id'));
$server = '&server_id='.Input::get('server_id');
}
// PAGINATION
$servers = $servers->paginate(5);
$pagination = $servers->appends(
array(
'game_id' => Input::get('game_id'),
'server_id' => Input::get('server_id'),
'sort' => Input::get('sort'),
'order' => Input::get('order')
))->links();
return View::make(servers.list)->with(
array(
'game' => null,
'servers' => $servers,
'pagination' => $pagination,
'querystr' => '&order='.(Input::get('order') == 'asc' || null ? 'desc' : 'asc').$game.$server
));
}