Tell us how to build a visual template in the paginator
- ivanscm
- Member | 16
Tell us how to use this code in the template to build a paginator:
$paginator = new \Nette\Utils\Paginator;
$paginator->setItemCount($this->pages->count());
$paginator->setItemsPerPage(1);
$paginator->setPage(1);
$this->template->pages_paginator = $paginator;
$this->template->pages = $this->pages->limit($paginator->getLength(), $paginator->getOffset());
The code for this paginator:
<div class="pagination">
<ul>
<li><a href="#">Prev</a></li>
<li class="active">
<a href="#">1</a>
</li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">Next</a></li>
</ul>
</div>
How to build a link to the page?
- Majkl578
- Moderator | 1364
I'd suggest you to use VisualPaginator. It is documented here (only in czech, but code is same). Most recent version is on Github.
Basically, you use the paginator wrapped inside a component, that handles its rendering (file template.phtml). The major benefit is the persistence of current page, provided by the component.
To create this component, add the factory to your presenter:
protected function createComponentVp()
{
return new \VisualPaginator();
}
Then, in an action of the presenter, you set the paginator's parameters:
$paginator = $this['vp']->getPaginator();
$paginator->itemsPerPage = 15; // number of visible items per page
$paginator->itemCount = count($items); // total count of all items to paginate
Lastly, you just render it in your template:
{control vp}
Now your visual paginator is ready and should work as expected.
PS: Depending on Nette you are using, you might need to adjust some
namespaces – Nette\Application\Control
→
Nette\Application\UI\Control
and Nette\Paginator
→
Nette\Utils\Paginator
.
Last edited by Majkl578 (2012-03-09 22:00)