Ad

Laravel 5.2 Pagination Target _blank

- 1 answer

I have the following code in Laravel to create pagination

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();
})

I can't find how you set a target for the links, I need the option to change between _blank, _parent and _self.

Ad

Answer

Following Mark Davidson's advice, I've ran through this quickly to see how it works.

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 target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="'.htmlentities($url).'"'.$rel.' target="'.$this->target.'">'.$page.'</a></li>';
    }
}

To use this, I simply do the following...

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

The links should receive the target attribute of whatever you pass into the constructor's 3rd parameter.

Ad
source: stackoverflow.com
Ad