pagination problem
iometrine opened this issue · 2 comments
Hello,
When you have a lot data, the paginator display page 1..paginator.lastpage.
So it displays all the pages, it shoud be great that display only 5 pages
so i propose something like that to your classe Pagination.php
private const SIDE_SIZE = 3;
public function getStart(): int
{
if ($this->currentPage<= self::SIDE_SIZE ){
return 1;
}
else
return $this->currentPage - self::SIDE_SIZE;
}
public function getEnd(): int
{
if ($this->currentPage<= self::SIDE_SIZE ){
return self::SIDE_SIZE*2+1;
}
elseif ($this->currentPage == (int) ceil($this->numResults / $this->pageSize) ){
return $this->currentPage;
}
else
return $this->currentPage+self::SIDE_SIZE;
}
and in your twig file
{% if paginator.hasToPaginate %}
<div class="navigation text-center">
<ul class="pagination">
{% if paginator.hasPreviousPage %}
<li class="prev"><a href="{{ path('search_paginated', {page: paginator.previousPage}) }}" rel="previous"><i class="fa fw fa-long-arrow-left"></i> Previous</a></li>
<li><a href="{{ path('search_paginated', {page: 1}) }}">start</a></li>
{% else %}
<li class="prev disabled"><span><i class="fa fw fa-arrow-left"></i> Previous</span></li>
{% endif %}
{% for i in paginator.start..paginator.end %}
{% if i == paginator.currentPage %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li><a href="{{ path('search_paginated', {page: i}) }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if paginator.hasNextPage %}
<li class="prev"><a href="{{ path('search_paginated', {page: paginator.nextPage}) }}" rel="next">Next <i class="fa fw fa-arrow-right"></i></a></li>
<li><a href="{{ path('search_paginated', {page: paginator.lastPage}) }}">end</a></li>
{% else %}
<li class="next disabled"><span>Next <i class="fa fw fa-arrow-right"></i></span></li>
{% endif %}
</ul>
</div>
{% endif %}
@iometrine thanks for your proposal. I've been thinking about this ... and at the end I've decided to not add this feature.
Given that this is a Demo project, we take some licenses to not complicate things too much. If Symfony had an official paginator, this feature would be mandatory ... but in this Demo app we just made a usable but not full-featured paginator ... so it's OK for simple and demo applications (it wouldn't be OK for a complex real app with thousands of pages).
Thanks for sharing the code of the proposed solution, because people needing that will be able to copy it into their projects. Thanks!