php Laravel 5 中的自定义分页视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28240777/
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
Custom pagination view in Laravel 5
提问by user2094178
Laravel 4.2has the option to specify a custom view in app/config/view.php
such as:
Laravel 4.2可以选择指定自定义视图,app/config/view.php
例如:
/*
|--------------------------------------------------------------------------
| Pagination View
|--------------------------------------------------------------------------
|
| This view will be used to render the pagination link output, and can
| be easily customized here to show any view you like. A clean view
| compatible with Twitter's Bootstrap is given to you by default.
|
*/
'pagination' => 'pagination_slider-alt'
This is gone in Laravel 5at least regarding view.php
.
这是在走了Laravel 5至少就view.php
。
Is there a way to replicate this behavior in Laravel 5?
有没有办法在Laravel 5 中复制这种行为?
采纳答案by user2094178
Whereas in Laravel 4.2I would use:
而在Laravel 4.2 中我会使用:
{{ $users->links('view.name') }}
In Laravel 5you can replicate the above with the following:
在Laravel 5 中,您可以使用以下内容复制上述内容:
@include('view.name', ['object' => $users])
Now in the included view, $object
will have the pagination methods available, such as currentPage()
, lastPage()
, perPage()
, etc.
现在,在包括视图,$object
将有可用的分页方法,如currentPage()
,lastPage()
,perPage()
,等。
You can view all methods available at http://laravel.com/docs/5.0/pagination
您可以在http://laravel.com/docs/5.0/pagination查看所有可用的方法
回答by Mantas D
In Laravel 5.3+ use
在Laravel 5.3+ 中使用
$users->links('view.name')
In Laravel 5.0 - 5.2instead of
在Laravel 5.0 - 5.2而不是
$users->render()
use
用
@include('pagination.default', ['paginator' => $users])
views/pagination/default.blade.php
视图/分页/default.blade.php
@if ($paginator->lastPage() > 1)
<ul class="pagination">
<li class="{{ ($paginator->currentPage() == 1) ? ' disabled' : '' }}">
<a href="{{ $paginator->url(1) }}">Previous</a>
</li>
@for ($i = 1; $i <= $paginator->lastPage(); $i++)
<li class="{{ ($paginator->currentPage() == $i) ? ' active' : '' }}">
<a href="{{ $paginator->url($i) }}">{{ $i }}</a>
</li>
@endfor
<li class="{{ ($paginator->currentPage() == $paginator->lastPage()) ? ' disabled' : '' }}">
<a href="{{ $paginator->url($paginator->currentPage()+1) }}" >Next</a>
</li>
</ul>
@endif
That's it.
就是这样。
If you have a lot of pages, use this template:
如果您有很多页面,请使用此模板:
views/pagination/limit_links.blade.php
视图/分页/limit_links.blade.php
<?php
// config
$link_limit = 7; // maximum number of links (a little bit inaccurate, but will be ok for now)
?>
@if ($paginator->lastPage() > 1)
<ul class="pagination">
<li class="{{ ($paginator->currentPage() == 1) ? ' disabled' : '' }}">
<a href="{{ $paginator->url(1) }}">First</a>
</li>
@for ($i = 1; $i <= $paginator->lastPage(); $i++)
<?php
$half_total_links = floor($link_limit / 2);
$from = $paginator->currentPage() - $half_total_links;
$to = $paginator->currentPage() + $half_total_links;
if ($paginator->currentPage() < $half_total_links) {
$to += $half_total_links - $paginator->currentPage();
}
if ($paginator->lastPage() - $paginator->currentPage() < $half_total_links) {
$from -= $half_total_links - ($paginator->lastPage() - $paginator->currentPage()) - 1;
}
?>
@if ($from < $i && $i < $to)
<li class="{{ ($paginator->currentPage() == $i) ? ' active' : '' }}">
<a href="{{ $paginator->url($i) }}">{{ $i }}</a>
</li>
@endif
@endfor
<li class="{{ ($paginator->currentPage() == $paginator->lastPage()) ? ' disabled' : '' }}">
<a href="{{ $paginator->url($paginator->lastPage()) }}">Last</a>
</li>
</ul>
@endif
回答by Joyal
For Laravel 5.3 (and may be in other 5.X versions) put custom pagination code in you view folder.
对于 Laravel 5.3(可能在其他 5.X 版本中),将自定义分页代码放在您的视图文件夹中。
resources/views/pagination/default.blade.php
资源/视图/分页/default.blade.php
@if ($paginator->hasPages())
<ul class="pagination">
{{-- Previous Page Link --}}
@if ($paginator->onFirstPage())
<li class="disabled"><span>«</span></li>
@else
<li><a href="{{ $paginator->previousPageUrl() }}" rel="prev">«</a></li>
@endif
{{-- Pagination Elements --}}
@foreach ($elements as $element)
{{-- "Three Dots" Separator --}}
@if (is_string($element))
<li class="disabled"><span>{{ $element }}</span></li>
@endif
{{-- Array Of Links --}}
@if (is_array($element))
@foreach ($element as $page => $url)
@if ($page == $paginator->currentPage())
<li class="active"><span>{{ $page }}</span></li>
@else
<li><a href="{{ $url }}">{{ $page }}</a></li>
@endif
@endforeach
@endif
@endforeach
{{-- Next Page Link --}}
@if ($paginator->hasMorePages())
<li><a href="{{ $paginator->nextPageUrl() }}" rel="next">»</a></li>
@else
<li class="disabled"><span>»</span></li>
@endif
</ul>
@endif
then call this pagination view file from the main view file as
然后从主视图文件中调用这个分页视图文件作为
{{ $posts->links('pagination.default') }}
Update the pagination/default.blade.php however you want
根据需要更新分页/default.blade.php
It works in 6.x versions as well.
它也适用于 6.x 版本。
回答by Javi Stolz
In Laravel 5 custom pagination is based on presenters (classes) instead of views.
在 Laravel 5 中自定义分页基于演示者(类)而不是视图。
Assuming in your routed code you have
假设在你的路由代码中你有
$users = Users::paginate(15);
In L4 you used to do something like this in your views:
在 L4 中,你曾经在视图中做这样的事情:
$users->appends(['sort' => 'votes'])->links();
In L5 you do instead:
在 L5 中,您改为:
$users->appends(['sort' => 'votes'])->render();
The render()
method accepts an Illuminate\Contracts\Pagination\Presenter
instance. You can create a custom class that implements that contract and pass it to the render()
method. Note that Presenter
is an interface, not a class, therefore you must implementit, not extendit. That's why you are getting the error.
该render()
方法接受一个Illuminate\Contracts\Pagination\Presenter
实例。您可以创建一个自定义类来实现该契约并将其传递给该render()
方法。请注意,这Presenter
是一个接口,而不是一个类,因此您必须实现它,而不是扩展它。这就是您收到错误的原因。
Alternatively you can extend the Laravel paginator (in order to use its pagination logic) and then pass the existing pagination instance ($users->...
) to you extended class constructor. This is indeed what I did for creating my custom Zurb Foundationpresenter based on the Bootstrap presenter provided by Laravel. It uses all the Laravel pagination logic and only overrides the rendering methods.
或者,您可以扩展 Laravel 分页器(以便使用其分页逻辑),然后将现有的分页实例 ( $users->...
) 传递给您的扩展类构造函数。这确实是我基于 Laravel 提供的 Bootstrap 演示器创建自定义Zurb Foundation演示器所做的工作。它使用所有 Laravel 分页逻辑并且只覆盖渲染方法。
With my custom presenter my views look like this:
使用我的自定义演示者,我的视图如下所示:
with(new \Stolz\Laravel\Pagination($users->appends(['sort' => 'votes'])))->render();
And my customized pagination presenter is:
我定制的分页演示者是:
<?php namespace Stolz\Laravel;
use Illuminate\Pagination\BootstrapThreePresenter;
class Pagination extends BootstrapThreePresenter
{
/**
* Convert the URL window into Zurb Foundation HTML.
*
* @return string
*/
public function render()
{
if( ! $this->hasPages())
return '';
return sprintf(
'<ul class="pagination" aria-label="Pagination">%s %s %s</ul></div>',
$this->getPreviousButton(),
$this->getLinks(),
$this->getNextButton()
);
}
/**
* Get HTML wrapper for disabled text.
*
* @param string $text
* @return string
*/
protected function getDisabledTextWrapper($text)
{
return '<li class="unavailable" aria-disabled="true"><a href="javascript:void(0)">'.$text.'</a></li>';
}
/**
* Get HTML wrapper for active text.
*
* @param string $text
* @return string
*/
protected function getActivePageWrapper($text)
{
return '<li class="current"><a href="javascript:void(0)">'.$text.'</a></li>';
}
/**
* Get a pagination "dot" element.
*
* @return string
*/
protected function getDots()
{
return $this->getDisabledTextWrapper('…');
}
}
回答by Vikas Dwivedi
In 5.5
links()
is replaced with render()
which appears to work similarly. [Official DOC]
In5.5
links()
被替换为render()
which 似乎工作类似。[官方文件]
replace
代替
{{ $replies->links() }}
with
和
{{ $replies->render("pagination::default") }}
Following commands will generate Pagination template in resources/views/vendor/pagination
以下命令将在 resources/views/vendor/pagination
artisan vendor:publish --tag=laravel-pagination
artisan vendor:publish
In any view file (blade files) you can use those template like following ex:
在任何视图文件(刀片文件)中,您都可以使用这些模板,例如:
{{ $replies->render("pagination::default") }}
{{ $replies->render("pagination::bootstrap-4") }}
{{ $replies->render("pagination::simple-bootstrap-4") }}
{{ $replies->render("pagination::semantic-ui") }}
{{ $replies->render("pagination::default") }}
{{ $replies->render("pagination::bootstrap-4") }}
{{ $replies->render("pagination::simple-bootstrap-4") }}
{{ $replies->render("pagination::semantic-ui") }}
回答by Morgan O'Neal
Laravel 5 ships with a Bootstrap 4 paginatorif anyone needs it.
如果有人需要,Laravel 5 附带了Bootstrap 4 分页器。
First create a new service provider.
首先创建一个新的服务提供者。
php artisan make:provider PaginationServiceProvider
In the register
method pass a closure to Laravel's paginator class that creates and returns the new presenter.
在该register
方法中,将一个闭包传递给 Laravel 的 paginator 类,该类创建并返回新的演示者。
<?php
namespace App\Providers;
use Illuminate\Pagination\BootstrapFourPresenter;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\ServiceProvider;
class PaginationServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
}
/**
* Register the application services.
*
* @return void
*/
public function register()
{
Paginator::presenter(function($paginator)
{
return new BootstrapFourPresenter($paginator);
});
}
}
Register the new provider in config/app.php
注册新的提供者 config/app.php
'providers' => [
//....
App\Providers\PaginationServiceProvider::class,
]
I found this example at Bootstrap 4 Pagination With Laravel
回答by CarlosCarucce
Maybe it is too late, but I would like to share another custom pagination template I made that creates a first/next and last/previous links. It also hides the links when the user is in the first/last page already.
也许为时已晚,但我想分享我制作的另一个自定义分页模板,该模板创建了第一个/下一个和最后一个/上一个链接。当用户已经在第一页/最后一页时,它也会隐藏链接。
(Optional)You can also determine the interval of links (the number of links before and after the current page)
(可选)还可以确定链接间隔(当前页面前后的链接数)
Usage example:
用法示例:
@include('pagination', ['paginator' => $users])
or
或者
@include('pagination', ['paginator' => $users, 'interval' => 5])
Here is the gist: https://gist.github.com/carloscarucce/33f6082d009c20f77499252b89c35dea
这是要点:https: //gist.github.com/carloscarucce/33f6082d009c20f77499252b89c35dea
And the code:
和代码:
@if (isset($paginator) && $paginator->lastPage() > 1)
<ul class="pagination">
<?php
$interval = isset($interval) ? abs(intval($interval)) : 3 ;
$from = $paginator->currentPage() - $interval;
if($from < 1){
$from = 1;
}
$to = $paginator->currentPage() + $interval;
if($to > $paginator->lastPage()){
$to = $paginator->lastPage();
}
?>
<!-- first/previous -->
@if($paginator->currentPage() > 1)
<li>
<a href="{{ $paginator->url(1) }}" aria-label="First">
<span aria-hidden="true">«</span>
</a>
</li>
<li>
<a href="{{ $paginator->url($paginator->currentPage() - 1) }}" aria-label="Previous">
<span aria-hidden="true">‹</span>
</a>
</li>
@endif
<!-- links -->
@for($i = $from; $i <= $to; $i++)
<?php
$isCurrentPage = $paginator->currentPage() == $i;
?>
<li class="{{ $isCurrentPage ? 'active' : '' }}">
<a href="{{ !$isCurrentPage ? $paginator->url($i) : '#' }}">
{{ $i }}
</a>
</li>
@endfor
<!-- next/last -->
@if($paginator->currentPage() < $paginator->lastPage())
<li>
<a href="{{ $paginator->url($paginator->currentPage() + 1) }}" aria-label="Next">
<span aria-hidden="true">›</span>
</a>
</li>
<li>
<a href="{{ $paginator->url($paginator->lastpage()) }}" aria-label="Last">
<span aria-hidden="true">»</span>
</a>
</li>
@endif
</ul>
@endif
回答by Peter
A quick JS fix for Bootstrap 4 pagination in Laravel 5+
Laravel 5+ 中 Bootstrap 4 分页的快速 JS 修复
Simply place the below script within your page:
只需将以下脚本放在您的页面中:
<script>
$('.pagination li').addClass('page-item');
$('.pagination li a').addClass('page-link');
$('.pagination span').addClass('page-link');
</script>
Advantages: saves server CPU, needs no adjustments in your app.
优点:节省服务器 CPU,无需在您的应用程序中进行调整。
回答by Meki
Laravel 5.2 uses presenters for this. You can create custom presenters or use the predefined ones. Laravel 5.2 uses the BootstrapThreePrensenter
out-of-the-box, but it's easy to use the BootstrapFroutPresenter
or any other custom presenters for that matter.
Laravel 5.2 为此使用演示器。您可以创建自定义演示者或使用预定义的演示者。Laravel 5.2 使用BootstrapThreePrensenter
开箱即用的,但很容易使用BootstrapFroutPresenter
或任何其他自定义演示器。
public function index()
{
return view('pages.guestbook',['entries'=>GuestbookEntry::paginate(25)]);
}
In your blade template, you can use the following formula:
在您的刀片模板中,您可以使用以下公式:
{!! $entries->render(new \Illuminate\Pagination\BootstrapFourPresenter($entries)) !!}
For creating custom presenters I recommend watching Codecourse's videoabout this.
为了创建自定义演示者,我建议观看Codecourse 的视频。
回答by Muamar Bin Kholil
if you want to beautify the appearance of your pagination, I use the class from bootstrap to make it more simple and easy
如果你想美化你的分页外观,我使用 bootstrap 中的类来使它更简单易行
@if ($students->lastPage() > 1)
<ul class="pagination ml-auto">
<li class="{{ ($students->currentPage() == 1) ? ' disabled' : '' }} page-item">
<a class=" page-link " href="{{ $students->url(1) }}" aria-label="Previous">
<span aria-hidden="true">«</span>
<span class="sr-only">Previous</span>
</a>
</li>
@for ($i = 1; $i <= $students->lastPage(); $i++)
<li class="{{ ($students->currentPage() == $i) ? ' active' : '' }} page-item">
<a class=" page-link " href="{{ $students->url($i) }}">{{ $i }}</a>
</li>
@endfor
<li class="{{ ($students->currentPage() == $students->lastPage()) ? ' disabled' : '' }} page-item">
<a href="{{ $students->url($students->currentPage()+1) }}" class="page-link" aria-label="Next">
<span aria-hidden="true">»</span>
<span class="sr-only">Next</span>
</a>
</li>
</ul>
@endif