Laravel 4:分页和排序标题

时间:2014-04-15 00:10:00

标签: php laravel laravel-4

我遇到了Laravel Pagination和Header Sort的问题,让我告诉你我的内容。

我正在使用的当前页面是" admin / admins"我有分页和标题。

1)AdminController代码:

// get for admin/admins page
// page to list all the Admin users
public function getAdmins($sort= NULL , $sort_dir = NULL) {

    // CACHE SORTING INPUTS
    $allowed = array('first_name', 'last_name', 'email', 'activated', 'created_at'); // add allowable columns to search on
    $sort = in_array($sort, $allowed) ? $sort : 'first_name'; // if user type in the url a column that doesn't exist app will default to first_name

    // header links setup.
    $params = Request::except(['sort','sort_dir']);
    $sort_dir = ($sort_dir == 'asc') ? 'desc' : 'asc';

    $i = 0 ;
    foreach ($allowed as $allow) {  
        $attributes[$i] = array_merge(['sort' => $allowed[$i], 'sort_dir' => $sort_dir], $params);
        $i++;
    }

    // select all admins Group = 1
    $admins = DB::table('users')
        ->join('users_roles', 'users.id', '=', 'users_roles.user_id')
        ->where('users_roles.role_id', '=' ,0)
        ->orderBy($sort, $sort_dir)
        ->paginate($this->perpage);

    // check for actions
    if (!is_null(Input::get('action'))) {

        $action = Input::get('action');

        if ($action == "add") {
             $this->layout->content = View::make('admin.admins-add');
        }
    } else {

         // get current counter admin counts
         $counter = $admins->getFrom();
         View::share('counter', $counter);

         View::share('attributes', $attributes);

         // share admin with template
         View::share('admins', $admins);

         $this->layout->content = View::make('admin.admins');
    }
}

2)route.php

Route::controller('admin', 'AdminController');

3)view/admin/admins.blade.php(生成标题链接):

<th>{{ link_to_action('AdminController@getAdmins', 'First Name', $attributes[0]) }}</th>
<th>{{ link_to_action('AdminController@getAdmins', 'Last Name' , $attributes[1]) }}</th>
<th>{{ link_to_action('AdminController@getAdmins', 'Email' , $attributes[2]) }}</th>
<th>{{ link_to_action('AdminController@getAdmins', 'Activated' , $attributes[3]) }}</th>
<th>{{ link_to_action('AdminController@getAdmins', 'Created' , $attributes[4]) }}</th>
<th>Actions</th>

{{ $admins->links() }}

4){{ $admins->links() }}将生成我们知道的分页链接

这是我的问题 生成的链接如下所示:

<tr>
  <th><a href="admin/admins/first_name/asc">First Name</a></th>
  <th><a href="admin/admins/last_name/asc">Last Name</a></th>
  <th><a href="admin/admins/email/asc">Email</a></th>
  <th><a href="admin/admins/activated/asc">Activated</a></th>
  <th><a href="admin/admins/created_at/asc">Created</a></th>
  <th>Actions</th>
</tr>

看起来很好,但是当你进入第二页时问题,生成的链接看起来像:

<tr>
  <th><a href="admin/admins/first_name/asc/2">First Name</a></th>
  <th><a href="admin/admins/last_name/asc/2">Last Name</a></th>
  <th><a href="admin/admins/email/asc/2">Email</a></th>
  <th><a href="admin/admins/activated/asc/2">Activated</a></th>
  <th><a href="admin/admins/created_at/asc/2">Created</a></th>
  <th>Actions</th>
</tr>

当我点击其中任何一个时,它会将我带到第一页并且分页不起作用。我怎么能解决这个问题?分页类没有干净的URL,如果我将URL设置为这样,它的唯一方法就是:

<tr>
  <th><a href="admin/admins/first_name/asc/?page=2">First Name</a></th>
  <th><a href="admin/admins/last_name/asc/?page=2">Last Name</a></th>
  <th><a href="admin/admins/email/asc/?page=2">Email</a></th>
  <th><a href="admin/admins/activated/asc/?page=2">Activated</a></th>
  <th><a href="admin/admins/created_at/asc/?page=2">Created</a></th>
  <th>Actions</th>
</tr>

1 个答案:

答案 0 :(得分:0)

也许你已经得到了解决方案,但我做到了这一点:

在您的控制器中:

$sort = Input::get('sort')=== '' ? 'id': Input::get('sort');
$sort_dir = Input::get('sort_dir') === 'asc' ? 'asc' : 'desc';

在您的视图/ admin / admins.blade.php中,您可以手动创建如下链接:

首先,定义$ page

@if($page = $admins->getCurrentPage() )@endif

然后

<th>
       @if ($sort == 'first_name' && $sort_dir == 'asc')
          <a href="{{ Request::url() }}?page={{$page}}&sort_dir=desc&sort={{$sort}}">First Name</a>
       @else
          <a href="{{ Request::url() }}?page={{$page}}&sort_dir=asc&sort={{$sort}}">First Name</a>
       @endif
</th>

必须是另一种方式,但这对我有用!