Skip to content

Commit

Permalink
add smart pagination
Browse files Browse the repository at this point in the history
to avoid excessively long page lists
  • Loading branch information
Torsten Rehn committed Apr 15, 2015
1 parent dc74fd1 commit 10c00f6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
Expand Up @@ -2,6 +2,7 @@
{% load humanize %}
{% load i18n %}
{% load staticfiles %}
{% load smart_pagination %}
{% block "title" %}{% trans "Browse" %}{% endblock %}
{% block "css" %}
<link href="{% static 'css/secrets.css' %}" rel="stylesheet">
Expand Down Expand Up @@ -61,7 +62,7 @@ <h1>
{% else %}
<li class="disabled"><a href="#"><i class="fa fa-chevron-left"></i></a></li>
{% endif %}
{% for page in paginator.page_range %}
{% for page in paginator.page_range|smart_pages:page_obj.number %}
{% if page_obj.number == page %}
<li class="active"><a href="#">{{ page }} <span class="sr-only">(current)</span></a></li>
{% else %}
Expand Down
Empty file.
20 changes: 20 additions & 0 deletions src/teamvault/apps/secrets/templatetags/smart_pagination.py
@@ -0,0 +1,20 @@
from django import template

register = template.Library()


@register.filter
def smart_pages(all_pages, current_page):
all_pages = list(all_pages)
smart_pages = set([
1,
all_pages[-1],
current_page,
max(min(current_page // 2, all_pages[-1]), 1),
max(min(current_page + ((all_pages[-1] - current_page) // 2), all_pages[-1]), 1),
max(min(current_page + 1, all_pages[-1]), 1),
max(min(current_page + 2, all_pages[-1]), 1),
max(min(current_page - 1, all_pages[-1]), 1),
max(min(current_page - 2, all_pages[-1]), 1),
])
return sorted(smart_pages)

0 comments on commit 10c00f6

Please sign in to comment.