1
0
mirror of https://github.com/djohnlewis/stackdump synced 2024-12-04 23:17:37 +00:00

For results with lots of pages, only a limited set of page numbers are rendered.

This commit is contained in:
Samuel Lai 2012-08-12 16:32:42 +10:00
parent 3944261eef
commit 3d515f51b1
2 changed files with 53 additions and 9 deletions

View File

@ -83,6 +83,33 @@ def set_get_parameters(base_url, *new_parameters):
return '%s?%s' % (base_url, '&'.join(parameters))
def get_surrounding_page_numbers(total_pages, cur_page, count=9):
"""
Returns a list of ints representing pages that surround the cur_page.
For example, if total_pages = 34, cur_page = 11, count = 9, then
[ 7, 8, 9, 10, 11, 12, 13, 14, 15 ] would be returned.
"""
# if we can show all the page links, show them all
if total_pages <= count:
return range(1, total_pages + 1)
# the -1 is for the current page in the middle
pages_per_side = int(math.floor((count - 1) / 2))
left_max = cur_page - pages_per_side
right_max = cur_page + pages_per_side
if left_max < 1:
diff = 1 - left_max
left_max = 1
right_max += diff
elif right_max > total_pages:
diff = right_max - total_pages
left_max -= diff
right_max = total_pages
return range(left_max, right_max + 1)
# END CUSTOM TEMPLATE TAGS AND FILTERS
@ -108,6 +135,7 @@ def uses_templates(fn):
)
thread_locals.template_env.filters['format_datetime'] = format_datetime
thread_locals.template_env.filters['set_get_parameters'] = set_get_parameters
thread_locals.template_env.filters['get_surrounding_page_numbers'] = get_surrounding_page_numbers
if not fn:
init_templates()

View File

@ -59,15 +59,31 @@
{% else %}
<li class="prev disabled"><a href="#">&larr; Previous</a></li>
{% endif %}
{% for p in range(1, total_pages + 1) %}
<li {% if p == current_page %}class="active"{% endif %}><a href="{{ REQUEST.url|set_get_parameters('p=' ~ (p-1)) }}">{{ p }}</a></li>
{% endfor %}
{% if current_page != total_pages %}
{# the next page is just current_page because current_page is ones-based, but the p GET parameter is zero-based #}
<li class="next"><a href="{{ REQUEST.url|set_get_parameters('p=' ~ current_page) }}">&rarr; Next</a></li>
{% else %}
<li class="next disabled"><a href="#">&rarr; Next</a></li>
{% endif %}
{% set page_numbers = total_pages|get_surrounding_page_numbers(current_page) %}
{% if page_numbers|first != 1 %}
<li class="disabled"><a href="#">...</a></li>
{% endif %}
{% for p in page_numbers %}
<li {% if p == current_page %}class="active"{% endif %}>
{% if p == current_page %}
{# this extra a tag is a hack to make bootstrap render properly. #}
<a>{{ p }}</a>
{% else %}
<a href="{{ REQUEST.url|set_get_parameters('p=' ~ (p-1)) }}">{{ p }}</a>
{% endif %}
</li>
{% endfor %}
{% if page_numbers|last != total_pages %}
<li class="disabled"><a href="#">...</a></li>
{% endif %}
{% if current_page != total_pages %}
{# the next page is just current_page because current_page is ones-based, but the p GET parameter is zero-based #}
<li class="next"><a href="{{ REQUEST.url|set_get_parameters('p=' ~ current_page) }}">&rarr; Next</a></li>
{% else %}
<li class="next disabled"><a href="#">&rarr; Next</a></li>
{% endif %}
</ul>
</div>
{% endif %}