Laravel 4:分页和排序标题



我在Laravel分页和头排序方面有问题,让我向您展示我的功能。

我目前使用的页面是"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>

也许那时你已经得到了解决方案,但我做了这个:

在您的控制器中:

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

在您的视图/admin/admins.lade.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>

肯定是另一种方式,但这对我很管用!

最新更新