Laravel 4:分页和排序标题 [英] Laravel 4 : Pagination and Sort Header
问题描述
我在Laravel分页和页眉排序方面遇到问题,让我告诉你我所拥有的.
I am having a problem with Laravel Pagination and Header Sort, Let me show you what I have.
我正在使用的当前页面是"admin/admins",其中有分页和标题.
My current page I am working with is "admin/admins" where i have pagination and headers.
1)AdminController代码:
1) AdminController code:
// 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
(用于生成标题链接):
3) view/admin/admins.blade.php
(To generate the header links):
<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() }}
将生成我们知道的分页链接
4) The {{ $admins->links() }}
will generate the pagination links as we know
这是我的问题 生成的链接看起来像这样:
HERE IS MY PROBLEM the generated links looks like this :
<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>
看起来不错,但是当您进入第二页时,生成的链接看起来像这样:
Which looks fine but the problem when you go on the 2nd page , the generated links looks like:
<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看起来像这样时,它才会起作用:
When I click on any of them it takes me to the first page and the pagination doesn't work. How could I fix this? The pagination class doesn't have clean URL, the only way it will work if I make my URL look like this:
<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>
推荐答案
也许您已经获得解决方案了,但是我做到了:
Maybe you already get the solution by the time, but I made this:
在您的控制器中:
$sort = Input::get('sort')=== '' ? 'id': Input::get('sort');
$sort_dir = Input::get('sort_dir') === 'asc' ? 'asc' : 'desc';
在您的view/admin/admins.blade.php中,您可以像这样手动创建链接:
In your view/admin/admins.blade.php you can manually create the link like this:
首先,定义$ page
First, define $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>
必须是另一种方式,但这对我有用!
Must to be another way, but this worked for me!
这篇关于Laravel 4:分页和排序标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!