Paginator: introducing "..." pseudo-pages
If delta between pages in paginator is greater than 2, it adds "..." inbetween. If delta is equal to 2, it adds missing page link instead. Examples: 1,4,5 gets converted to 1,...,4,5 1,3,4 gets converted to 1,2,3,4
This commit is contained in:
parent
467f0c6b93
commit
6e9a18c0ae
2 changed files with 50 additions and 33 deletions
|
@ -10,12 +10,12 @@
|
||||||
|
|
||||||
.paginator li {
|
.paginator li {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
|
margin-right: 0.5em;
|
||||||
}
|
}
|
||||||
|
|
||||||
.paginator li a {
|
.paginator li a {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
padding: 0.2em 0.5em;
|
padding: 0.2em 0.5em;
|
||||||
margin-right: 0.5em;
|
|
||||||
background: #eee;
|
background: #eee;
|
||||||
border: 1px solid silver;
|
border: 1px solid silver;
|
||||||
color: black;
|
color: black;
|
||||||
|
|
|
@ -1,13 +1,27 @@
|
||||||
<?php
|
<?php
|
||||||
$pagesVisible = [];
|
$page = $this->context->transport->paginator->page;
|
||||||
$pagesVisible []= 1;
|
$pageCount = $this->context->transport->paginator->pageCount;
|
||||||
$pagesVisible []= $this->context->transport->paginator->pageCount;
|
|
||||||
$delta = 3;
|
$delta = 3;
|
||||||
$pagesVisible = array_merge($pagesVisible, range($this->context->transport->paginator->page - $delta, $this->context->transport->paginator->page + $delta));
|
$pagesVisible = [1, $pageCount];
|
||||||
$pagesVisible = array_filter($pagesVisible, function($x) { return $x >= 1 and $x <= $this->context->transport->paginator->pageCount; });
|
$pagesVisible = array_merge($pagesVisible, range($page - $delta, $page + $delta));
|
||||||
|
$pagesVisible = array_filter($pagesVisible, function($x) use ($pageCount) { return $x >= 1 and $x <= $pageCount; });
|
||||||
$pagesVisible = array_unique($pagesVisible);
|
$pagesVisible = array_unique($pagesVisible);
|
||||||
sort($pagesVisible, SORT_NUMERIC);
|
sort($pagesVisible, SORT_NUMERIC);
|
||||||
|
|
||||||
|
$finalPages = [$pagesVisible[0]];
|
||||||
|
for ($i = 1; $i < count($pagesVisible); $i ++)
|
||||||
|
{
|
||||||
|
$prevPage = $pagesVisible[$i - 1];
|
||||||
|
$subPage = $pagesVisible[$i];
|
||||||
|
if ($subPage - $prevPage == 2)
|
||||||
|
$finalPages []= $subPage - 1;
|
||||||
|
elseif ($subPage - $prevPage > 2)
|
||||||
|
$finalPages []= null;
|
||||||
|
$finalPages []= $subPage;
|
||||||
|
}
|
||||||
|
$pagesVisible = $finalPages;
|
||||||
|
|
||||||
if (!function_exists('pageUrl'))
|
if (!function_exists('pageUrl'))
|
||||||
{
|
{
|
||||||
function pageUrl($page)
|
function pageUrl($page)
|
||||||
|
@ -15,8 +29,7 @@
|
||||||
$context = \Chibi\Registry::getContext();
|
$context = \Chibi\Registry::getContext();
|
||||||
$controller = $context->route->simpleControllerName;
|
$controller = $context->route->simpleControllerName;
|
||||||
$action = $context->route->simpleActionName;
|
$action = $context->route->simpleActionName;
|
||||||
$page = max(1, $page);
|
$page = max(1, min($context->transport->paginator->pageCount, $page));
|
||||||
$page = min($context->transport->paginator->pageCount, $page);
|
|
||||||
$params = $context->route->arguments;
|
$params = $context->route->arguments;
|
||||||
$params['page'] = $page;
|
$params['page'] = $page;
|
||||||
return \Chibi\UrlHelper::route($controller, $action, $params);
|
return \Chibi\UrlHelper::route($controller, $action, $params);
|
||||||
|
@ -27,34 +40,38 @@
|
||||||
<?php if (!empty($pagesVisible)): ?>
|
<?php if (!empty($pagesVisible)): ?>
|
||||||
<nav class="paginator-wrapper">
|
<nav class="paginator-wrapper">
|
||||||
<ul class="paginator">
|
<ul class="paginator">
|
||||||
<?php if ($this->context->transport->paginator->page > 1): ?>
|
<?php if ($page > 1): ?>
|
||||||
<li class="prev">
|
<li class="prev">
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
<li class="prev disabled">
|
<li class="prev disabled">
|
||||||
<?php endif ?>
|
<?php endif ?>
|
||||||
<a href="<?php echo pageUrl($this->context->transport->paginator->page - 1) ?>">
|
<a href="<?php echo pageUrl($page - 1) ?>">
|
||||||
«
|
«
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
|
|
||||||
<?php foreach ($pagesVisible as $page): ?>
|
<?php foreach ($pagesVisible as $subPage): ?>
|
||||||
<?php if ($page == $this->context->transport->paginator->page): ?>
|
<?php if ($subPage === null): ?>
|
||||||
|
<li>…</li>
|
||||||
|
<?php else: ?>
|
||||||
|
<?php if ($subPage == $page): ?>
|
||||||
<li class="active">
|
<li class="active">
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
<li>
|
<li>
|
||||||
<?php endif ?>
|
<?php endif ?>
|
||||||
<a href="<?php echo pageUrl($page) ?>">
|
<a href="<?php echo pageUrl($subPage) ?>">
|
||||||
<?php echo $page ?>
|
<?php echo $subPage ?>
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
|
<?php endif ?>
|
||||||
<?php endforeach ?>
|
<?php endforeach ?>
|
||||||
|
|
||||||
<?php if ($this->context->transport->paginator->page < $this->context->transport->paginator->pageCount): ?>
|
<?php if ($page < $pageCount): ?>
|
||||||
<li class="next">
|
<li class="next">
|
||||||
<?php else: ?>
|
<?php else: ?>
|
||||||
<li class="next disabled">
|
<li class="next disabled">
|
||||||
<?php endif ?>
|
<?php endif ?>
|
||||||
<a href="<?php echo pageUrl($this->context->transport->paginator->page + 1) ?>">
|
<a href="<?php echo pageUrl($page + 1) ?>">
|
||||||
»
|
»
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
|
|
Loading…
Reference in a new issue