Lov*_*ock 20 php pagination laravel
有什么简单的方法来限制Laravels分页显示的链接数量?
目前它最多显示13个链接(上一个,1 2 3 4 5 7 8 .. 78 79下一个)
然而,这对于移动设备来说太多了,并且变成了双线导航...有没有办法将链接设置为例如只显示10?
我和分页主持人搞砸了,但实际上似乎没什么用.
谢谢
Eri*_*ite 18
我使用 css 来限制我允许的链接。真的很简单……这可以扩展到在任意数量的断点处显示任意数量的页面
@media screen and ( max-width: 400px ){
li.page-item {
display: none;
}
.page-item:first-child,
.page-item:nth-child( 2 ),
.page-item:nth-last-child( 2 ),
.page-item:last-child,
.page-item.active,
.page-item.disabled {
display: block;
}
}
Run Code Online (Sandbox Code Playgroud)
这个特定的实现允许箭头,'...',第一页,活动页和最后一页
小智 8
对于 Laravel 5.6+
发布供应商模板:
php artisan vendor:publish --tag=laravel-pagination
Run Code Online (Sandbox Code Playgroud)
编辑bootstrap-4.blade.php如下:
@if ($paginator->hasPages())
<ul class="pagination" role="navigation">
{{-- Previous Page Link --}}
@if ($paginator->onFirstPage())
<li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.previous')">
<span class="page-link" aria-hidden="true">‹</span>
</li>
@else
<li class="page-item">
<a class="page-link" href="{{ $paginator->previousPageUrl() }}" rel="prev" aria-label="@lang('pagination.previous')">‹</a>
</li>
@endif
<?php
$start = $paginator->currentPage() - 2; // show 3 pagination links before current
$end = $paginator->currentPage() + 2; // show 3 pagination links after current
if($start < 1) {
$start = 1; // reset start to 1
$end += 1;
}
if($end >= $paginator->lastPage() ) $end = $paginator->lastPage(); // reset end to last page
?>
@if($start > 1)
<li class="page-item">
<a class="page-link" href="{{ $paginator->url(1) }}">{{1}}</a>
</li>
@if($paginator->currentPage() != 4)
{{-- "Three Dots" Separator --}}
<li class="page-item disabled" aria-disabled="true"><span class="page-link">...</span></li>
@endif
@endif
@for ($i = $start; $i <= $end; $i++)
<li class="page-item {{ ($paginator->currentPage() == $i) ? ' active' : '' }}">
<a class="page-link" href="{{ $paginator->url($i) }}">{{$i}}</a>
</li>
@endfor
@if($end < $paginator->lastPage())
@if($paginator->currentPage() + 3 != $paginator->lastPage())
{{-- "Three Dots" Separator --}}
<li class="page-item disabled" aria-disabled="true"><span class="page-link">...</span></li>
@endif
<li class="page-item">
<a class="page-link" href="{{ $paginator->url($paginator->lastPage()) }}">{{$paginator->lastPage()}}</a>
</li>
@endif
{{-- Next Page Link --}}
@if ($paginator->hasMorePages())
<li class="page-item">
<a class="page-link" href="{{ $paginator->nextPageUrl() }}" rel="next" aria-label="@lang('pagination.next')">›</a>
</li>
@else
<li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.next')">
<span class="page-link" aria-hidden="true">›</span>
</li>
@endif
</ul>
@endif
Run Code Online (Sandbox Code Playgroud)
此示例以正确的方式处理新的 CSS 类、活动链接、三个点(正确地,不是 1..2 3 4)并且是否可自定义(您要显示的页面数)。
现在,Laravel 5.7具有新的分页方法,可自定义分页器两侧的链接数。由于有了新方法,在某些情况下您不再需要自定义分页视图。您可以使用以下API在当前页面的每一侧定义链接计数:
User::paginate(10)->onEachSide(2);
Run Code Online (Sandbox Code Playgroud)
在您的控制器上编写该代码。
您可以在https://laravel-news.com/laravel-5-7-pagination-link-customizations中查看更多详细信息
小智 6
来自 laravel 8 文档。如果需要,您可以使用onEachSide刀片中的方法控制当前页面每一侧显示的附加链接数量:
{{ $posts->onEachSide(1)->links() }}
Run Code Online (Sandbox Code Playgroud)
我创建了一个新的自定义演示者,只显示10个链接.它涉及3个步骤:
创建自己的自定义演示者
use Illuminate\Pagination\BootstrapPresenter;
class CustomPresenter extends BootstrapPresenter{
protected function getPageSlider()
{
// Changing the original value from 6 to 3 to reduce the link count
$window = 3;
// If the current page is very close to the beginning of the page range, we will
// just render the beginning of the page range, followed by the last 2 of the
// links in this list, since we will not have room to create a full slider.
if ($this->currentPage <= $window)
{
$ending = $this->getFinish();
return $this->getPageRange(1, $window + 2).$ending;
}
// If the current page is close to the ending of the page range we will just get
// this first couple pages, followed by a larger window of these ending pages
// since we're too close to the end of the list to create a full on slider.
elseif ($this->currentPage >= $this->lastPage - $window)
{
$start = $this->lastPage - 8;
$content = $this->getPageRange($start, $this->lastPage);
return $this->getStart().$content;
}
// If we have enough room on both sides of the current page to build a slider we
// will surround it with both the beginning and ending caps, with this window
// of pages in the middle providing a Google style sliding paginator setup.
else
{
$content = $this->getAdjacentRange();
return $this->getStart().$content.$this->getFinish();
}
}
}
Run Code Online (Sandbox Code Playgroud)
创建自己的分页视图(例如custom-paginator.php),放在views文件夹中
<ul class="pagination">
<?php echo with(new CustomPresenter($paginator))->render(); ?>
</ul>
Run Code Online (Sandbox Code Playgroud)
更新你的app/config.view.php
'pagination' => 'custom-paginator',
Run Code Online (Sandbox Code Playgroud)
通过进行以下更改,您将能够获得10个链接分页符.
希望这有帮助:D
定义自定义演示者的旧方法不适用于 Laravel 5.3+,显示的链接数量似乎是硬编码在参数$onEachSide中Illuminate/Pagination/UrlWindow::make():
public static function make(PaginatorContract $paginator, $onEachSide = 3)
Run Code Online (Sandbox Code Playgroud)
我最终只是编写了自己的 render() 函数,窃取了一些代码LengthAwarePaginator
/**
* Stole come code from LengthAwarePaginator::render() and ::elements() to allow for a smaller UrlWindow
*
* @param LengthAwarePaginator $paginator
* @param int $onEachSide
* @return string
*/
public static function render(LengthAwarePaginator $paginator, $onEachSide = 2)
{
$window = UrlWindow::make($paginator, $onEachSide);
$elements = array_filter([
$window['first'],
is_array($window['slider']) ? '...' : null,
$window['slider'],
is_array($window['last']) ? '...' : null,
$window['last'],
]);
return LengthAwarePaginator::viewFactory()->make(LengthAwarePaginator::$defaultView, [
'paginator' => $paginator,
'elements' => $elements,
])->render();
}
}
Run Code Online (Sandbox Code Playgroud)
我们使用 Twig,所以我将其注册为 Twig 过滤器,我想可以为 Blade 做类似的事情。