Create a page list for pagination with the logarithmic scale, neighbor pages or all pages.
Algorithms are copied from https://github.com/nikolassv/pagination.
The recommended way to install Forrest79/Pagination is through Composer:
composer require forrest79/pagination
Just call PagesFactory::
with the page list you want:
$pages = Forrest79\Pagination\PagesFactory::all(100);
$pages = Forrest79\Pagination\PagesFactory::neighbour(100, 1, 5);
$pages = Forrest79\Pagination\PagesFactory::logarithmic(100, 10, 10);
You will get sorted array
with integer
pages numbers. For neighbor and logarithmic scale, there are also NULL
values at place, where is broken pages series, for example: [1, 2, 3, NULL, 7, 8]
. So you know where to print space. You can disable this behavior by settings parameter $addGaps
to FALSE
.
Simple using with default paginator:
class Paginator extends Nette\Utils\Paginator
{
public function pages(): array
{
if ($this->getPageCount() === NULL) {
throw new InvalidArgumentException('We need page count set to generate pages list');
}
return Forrest79\Pagination\PagesFactory::logarithmic($this->getPageCount(), $this->getPage(), 10);
}
}
And in latte
:
<li n:foreach="$paginator->pages() as $page" n:class="$page === $paginator->getPage() ? active, $page === NULL ? disabled">
{if $page === NULL}
..
{else}
<a n:href="this, page => $page">{$page}</a>
{/if}
</li>