Laravel 5分页定制

时间:2015-04-04 11:25:50

标签: php pagination laravel-5

在Laravel 5中,我使用了docs中概述的simplePagination。我想自定义输出,而不是双V形& rdaquo; '>>',我可以放右箭头。但是我无法在任何地方看到自定义它。

有谁知道这方面的文件在哪里?或者从哪里开始寻找?

1 个答案:

答案 0 :(得分:1)

虽然没有证件,但肯定是可能的。它与Laravel 4几乎相同。基本上你需要的是创建一个自定义的演示者并包装paginator实例。

以下是演示者的样子:

use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Contracts\Pagination\Presenter;
use Illuminate\Pagination\BootstrapThreeNextPreviousButtonRendererTrait;
use Illuminate\Pagination\UrlWindow;
use Illuminate\Pagination\UrlWindowPresenterTrait;

class CustomPresenter implements Presenter
{
    use BootstrapThreeNextPreviousButtonRendererTrait, UrlWindowPresenterTrait;

    private $paginator;

    private $window;

    public function __construct(Paginator $paginator, UrlWindow $window = null)
    {
        $this->paginator = $paginator;
        $this->window = is_null($window) ? UrlWindow::make($paginator) : $window->get();
    }

    public function render()
    {
        if ($this->hasPages()) {
            return sprintf(
                '<ul class="pagination">%s %s %s</ul>',
                $this->getPreviousButton("Previous"),
                $this->getLinks(),
                $this->getNextButton("Next")
            );
        }

        return null;
    }

    public function hasPages()
    {
        return $this->paginator->hasPages() && count($this->paginator->items() !== 0);
    }

    protected function getDisabledTextWrapper($text)
    {
        return '<li class="disabled"><span>'.$text.'</span></li>';
    }

    protected function getActivePageWrapper($text)
    {
        return '<li class="active"><span>'.$text.'</span></li>';
    }

    protected function getDots()
    {
        return $this->getDisabledTextWrapper("...");
    }

    protected function currentPage()
    {
        return $this->paginator->currentPage();
    }

    protected function lastPage()
    {
        return $this->paginator->lastPage();
    }

    protected function getAvailablePageWrapper($url, $page, $rel = null)
    {
        $rel = is_null($rel) ? '' : ' rel="'.$rel.'"';

        return '<li><a href="'.htmlentities($url).'"'.$rel.'>'.$page.'</a></li>';
    }
}

然后从您的控制器:

    public function index()
    {
        $users = User::paginate(5);
        $presenter = new CustomPresenter($users);

        return view("home.index")->with(compact('users', 'presenter'));
    }

观点:

@foreach ($users as $user)
    <div>{{ $user->email }}</div>
@endforeach
{!! $presenter->render() !!}