【问题标题】:In Laravel Pagination how to add first page and last page?在 Laravel 分页中如何添加第一页和最后一页?
【发布时间】:2021-06-06 00:50:48
【问题描述】:

在 Laravel 8 分页中如何显示更少的页数以及如何添加“第一页”和“最后一页”

现在在 Laravel 中,当我们有更多数据时,它会显示这样的分页:

但我希望我的分页像下面给出的图像:

或者

【问题讨论】:

标签: php laravel pagination laravel-8


【解决方案1】:
<?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('&lsaquo;').$content.$this->getNext('&rsaquo;').$this->getLast();
    }

    public function getFirst($text = '&laquo;')
    {
        if ($this->currentPage <= 1)
        {
            return $this->getDisabledTextWrapper($text);
        }
        else
        {
            $url = $this->paginator->getUrl(1);
            return $this->getPageLinkWrapper($url, $text);
        }
    }

    public function getLast($text = '&raquo;')
    {
        if ($this->currentPage >= $this->lastPage)
        {
            return $this->getDisabledTextWrapper($text);
        }
        else
        {
            $url = $this->paginator->getUrl($this->lastPage);

            return $this->getPageLinkWrapper($url, $text);
        }
    }
}

【讨论】:

  • MyPresenter 类名,并在您的自定义视图中
      render(); ?>
【解决方案2】:

要自定义分页视图,请在控制台中运行以下命令

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">&laquo;</span>
        </li>
    @else
        <li class="page-item">
            <a class="page-link" href="{{ \Request::url() }}" rel="prev" aria-label="@lang('pagination.first')">&laquo;</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')">&raquo;</a>
        </li>
    @else
        <li class="page-item disabled" aria-disabled="true" aria-label="@lang('pagination.last')">
            <span class="page-link" aria-hidden="true">&raquo;</span>
        </li>
    @endif
</ul>

【讨论】:

    猜你喜欢
    • 2020-07-10
    • 2021-05-14
    • 2018-01-24
    • 1970-01-01
    • 2016-08-10
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    相关资源
    最近更新 更多