Laravel 5.2分页目标_blank

时间:2016-01-21 15:00:32

标签: php laravel pagination

我在Laravel中有以下代码来创建分页

Route::get('hede', function(){
    $paginator = new Illuminate\Pagination\LengthAwarePaginator(
        range(1,500), //a fake range of total items, you can use range(1, count($collection))
        500, //count as in 1st parameter
        20, //items per page
        \Illuminate\Pagination\Paginator::resolveCurrentPage(), //resolve the path
        ['path' => \Illuminate\Pagination\Paginator::resolveCurrentPath()]
    );

    return $paginator->render();
})

我无法找到如何为链接设置目标,我需要在_blank,_parent和_self之间进行更改。

2 个答案:

答案 0 :(得分:3)

您需要做的是为分页渲染创建自定义演示者。

如果你查看了这篇文章http://laravelista.com/laravel-custom-pagination-presenter/,它应该有助于你需要做什么。

基本上,您将创建演示者,并且您希望覆盖getPageLinkWrapper以在链接标记上引入目标参数的使用。

答案 1 :(得分:2)

按照马克戴维森的建议,我已经快速了解了它是如何运作的。

class CustomPresenter extends \Illuminate\Pagination\BootstrapThreePresenter
{
    protected $target;

    /**
     * Create a new Bootstrap presenter instance.
     *
     * @param  \Illuminate\Contracts\Pagination\Paginator  $paginator
     * @param  \Illuminate\Pagination\UrlWindow|null  $window
     * @param  string $target
     */
    public function __construct( \Illuminate\Contracts\Pagination\Paginator $paginator, \Illuminate\Pagination\UrlWindow $window = null, $target = '_self')
    {
        parent::__construct($paginator, $window);
        $this->target = $target;
    }

    /**
     * Get HTML wrapper for an available page link.
     *
     * @param  string  $url
     * @param  int  $page
     * @param  string|null  $rel
     * @return string
     */
    protected function getAvailablePageWrapper($url, $page, $rel = null)
    {
        $rel = is_null($rel) ? '' : ' rel="'.$rel.'"';

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

要使用此功能,我只需执行以下操作...

$items = Lotpro\User::all();
$paginator = new \Illuminate\Pagination\LengthAwarePaginator($items, $items->count(), 10, 0);
$presenter = new CustomPresenter($paginator, null, '_self');
echo $presenter->render();

链接应该接收你传递给构造函数的第三个参数的目标属性。