Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pagination in combination with filters #89

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/enquiries/templates/enquiry_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ <h2 class="govuk-heading-l">Enquiries</h2>
<ol class="pagination__list">
{%if previous is not None %}
<li class="pagination__list-item">
<a class="pagination__link" href="{{ previous }}">Previous</a>
<a class="pagination__link" href="{% url 'enquiry-list' %}?{% query_params_with_previous_page query_params current_page %}">Previous</a>
</li>
{%endif%}

{% for page in page_range %}
{% if page|stringformat:"s" == current_page %}
<a class="pagination__link" href="{% url 'enquiry-list' %}?page={{ page }}"><li class="pagination__list-item pagination__list-item-current">{{ page }}</li></a>
<a class="pagination__link" href="{% url 'enquiry-list' %}?{% query_params_with_pagination query_params page %}"><li class="pagination__list-item pagination__list-item-current">{{ page }}</li></a>
{% else %}
<a class="pagination__link" href="{% url 'enquiry-list' %}?page={{ page }}"><li class="pagination__list-item">{{ page }}</li></a>
<a class="pagination__link" href="{% url 'enquiry-list' %}?{% query_params_with_pagination query_params page %}"><li class="pagination__list-item">{{ page }}</li></a>
{% endif %}
{% endfor %}

{%if next is not None %}
<li class="pagination__list-item">
<a class="pagination__link" href="{{ next }}">Next</a>
<a class="pagination__link" href="{% url 'enquiry-list' %}?{% query_params_with_next_page query_params current_page page_range|length %}">Next</a>
</li>
{%endif%}
</ol>
Expand Down
28 changes: 28 additions & 0 deletions app/enquiries/templatetags/enquiries_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,31 @@ def query_params_has_value(value, param_key, query_params):
@register.simple_tag
def query_params_value_selected(value, param_key, query_params, text='selected'):
return f' {text}' if str(value) in query_params.getlist(param_key) else ''


@register.simple_tag

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these tag functions need some tests.

def query_params_with_pagination(query_params, page):
filter_params = '&'.join([f"{key}={value}" for key, value in query_params.items() if key != "page"])

if page and filter_params:
return f"page={page}&{filter_params}"
elif filter_params:
return filter_params
else:
return f"page={page}"


@register.simple_tag
def query_params_with_previous_page(query_params, current_page):
if current_page > 1:
current_page = current_page - 1

return query_params_with_pagination(query_params, current_page)


@register.simple_tag
def query_params_with_next_page(query_params, current_page, total_pages):
if current_page < total_pages:
current_page = int(current_page) + 1

return query_params_with_pagination(query_params, current_page)