【发布时间】:2021-06-06 00:50:48
【问题描述】:
【问题讨论】:
-
使用 simplePaginate 或自定义分页视图laravel.com/docs/8.x/pagination#customizing-the-pagination-view
-
我知道,但我的问题是我们在 Laravel 中没有内置和自定义的分页。
标签: php laravel pagination laravel-8
【问题讨论】:
标签: php laravel pagination laravel-8
<?php
class MyPresenter extends Illuminate\Pagination\BootstrapPresenter {
public function render()
{
if ($this->currentPage == 1) {
$content = $this->getPageRange(1, $this->currentPage + 2);
}
else if ($this->currentPage >= $this->lastPage) {
$content = $this->getPageRange($this->currentPage - 2, $this->lastPage);
}
else {
$content = $this->getPageRange($this->currentPage - 1, $this->currentPage + 1);
}
return $this->getFirst().$this->getPrevious('‹').$content.$this->getNext('›').$this->getLast();
}
public function getFirst($text = '«')
{
if ($this->currentPage <= 1)
{
return $this->getDisabledTextWrapper($text);
}
else
{
$url = $this->paginator->getUrl(1);
return $this->getPageLinkWrapper($url, $text);
}
}
public function getLast($text = '»')
{
if ($this->currentPage >= $this->lastPage)
{
return $this->getDisabledTextWrapper($text);
}
else
{
$url = $this->paginator->getUrl($this->lastPage);
return $this->getPageLinkWrapper($url, $text);
}
}
}
【讨论】:
要自定义分页视图,请在控制台中运行以下命令
php artisan vendor:publish --tag=laravel-pagination
在 /resources/views/vendor/pagination/bootstrap-4.blade.php 添加波纹管代码。
<ul class="pagination">
@if ($paginator->onFirstPage())
<li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.first')">
<span class="page-link" aria-hidden="true">«</span>
</li>
@else
<li class="page-item">
<a class="page-link" href="{{ \Request::url() }}" rel="prev" aria-label="@lang('pagination.first')">«</a>
</li>
@endif
...
@if ($paginator->hasMorePages())
<li class="page-item">
<a class="page-link" href="{{ \Request::url().'?page='.$paginator->lastPage() }}" rel="last" aria-label="@lang('pagination.last')">»</a>
</li>
@else
<li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.last')">
<span class="page-link" aria-hidden="true">»</span>
</li>
@endif
</ul>
【讨论】: