Laravel(3)Pagination Sorting&过滤

时间:2013-03-05 03:19:15

标签: php sorting filtering laravel laravel-3

我列出了所有"服务器"在我的"服务器"在我的视图中以分页返回的表格。我一直在努力弄清楚如何排序(如果可能的话,asc& desc)和过滤(在结果中搜索)工作。

这是我的控制器代码:

 $servers = Server::paginate(5);
 return View::make('servers.list')
            ->with('game', '')
            ->with('servers', $servers);

以下是我的排序视图代码:

<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>

我希望通过简单的锚链接完成排序,点击Votes或Rank或Date将返回带有该排序的数据。单击当前选定的相同排序锚将反转排序方向。

我也有一堆&#34;过滤器&#34;应用的类别和整数范围等选项将&#34;过滤&#34; /搜索表格并返回结果与过滤器前选择的排序相同。

分页类中是否所有/任何一个都可能?如果不是我的方式可能是什么?使用laravel时不太确定最好的方法。

1 个答案:

答案 0 :(得分:12)

由于伟大的分页和输入课程,这在laravel中很容易做到。在你的观点中试试这个:

示例视图

清理视图

<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;?>

示例控制器

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