Add a table_header_label template tag for page listing headers

This commit is contained in:
Matt Westcott 2019-03-07 20:36:13 +00:00 committed by Matt Westcott
parent da55e4bca7
commit 388c8231c1
2 changed files with 61 additions and 27 deletions

View file

@ -29,43 +29,31 @@ ordering: the current sort parameter
</th>
{% endif %}
<th class="title">
{% if sortable %}
<a href="{% if ordering == 'title' %}{% querystring ordering='-title' %}{% else %}{% querystring ordering='title' %}{% endif %}" class="icon icon-arrow-{% if ordering == 'title' %}down-after{% elif ordering == '-title' %}up-after{% else %}down-after{% endif %} {% if ordering == 'title' or ordering == '-title' %}teal{% endif %}">
{% trans 'Title' %}
</a>
{% else %}
{% trans 'Title' %}
{% endif %}
{% trans 'Title' as title_label %}
{% table_header_label label=title_label sortable=sortable sort_field='title' %}
</th>
{% if show_parent %}
<th class="parent">{% trans 'Parent' %}</th>
<th class="parent">
{% trans 'Parent' as parent_label %}
{% table_header_label label=parent_label sortable=0 %}
</th>
{% endif %}
<th class="updated">
{% if sortable %}
<a href="{% if ordering == 'latest_revision_created_at' %}{% querystring ordering='-latest_revision_created_at' %}{% else %}{% querystring ordering='latest_revision_created_at' %}{% endif %}" class="icon icon-arrow-{% if ordering == '-latest_revision_created_at' %}up-after{% else %}down-after{% endif %} {% if ordering == 'latest_revision_created_at' or ordering == '-latest_revision_created_at' %}teal {% endif %}">
{% trans 'Updated' %}
</a>
{% else %}
{% trans 'Updated' %}
{% endif %}
{% trans 'Updated' as updated_label %}
{% table_header_label label=updated_label sortable=sortable sort_field='latest_revision_created_at' %}
</th>
<th class="type">
{% trans 'Type' as type_label %}
{% if sortable and sortable_by_type %}
<a href="{% if ordering == 'content_type' %}{% querystring ordering='-content_type' %}{% else %}{% querystring ordering='content_type' %}{% endif %}" class="icon icon-arrow-{% if ordering == '-content_type' %}up-after{% else %}down-after{% endif %} {% if ordering == 'content_type' or ordering == '-content_type' %}teal {% endif %}">
{% trans 'Type' %}
</a>
{% table_header_label label=type_label sortable=1 sort_field='content_type' %}
{% else %}
{% trans 'Type' %}
{% table_header_label label=type_label sortable=0 %}
{% endif %}
</th>
<th class="status">
{% if sortable %}
<a href="{% if ordering == 'live' %}{% querystring ordering='-live' %}{% else %}{% querystring ordering='live' %}{% endif %}" class="icon icon-arrow-{% if ordering == '-live' %}up-after{% else %}down-after{% endif %} {% if ordering == 'live' or ordering == '-live' %}teal {% endif %}">
{% trans 'Status' %}
</a>
{% else %}
{% trans 'Status' %}
{% endif %}
{% trans 'Status' as status_label %}
{% table_header_label label=status_label sortable=sortable sort_field='live' %}
</th>
<th></th>
</tr>

View file

@ -8,7 +8,7 @@ from django.contrib.messages.constants import DEFAULT_TAGS as MESSAGE_TAGS
from django.template.defaultfilters import stringfilter
from django.template.loader import render_to_string
from django.templatetags.static import static
from django.utils.html import conditional_escape
from django.utils.html import conditional_escape, format_html
from django.utils.safestring import mark_safe
from wagtail.admin.menu import admin_menu
@ -289,6 +289,52 @@ def querystring(context, **kwargs):
return '?' + querydict.urlencode()
@register.simple_tag(takes_context=True)
def table_header_label(context, label=None, sortable=True, ordering=None, sort_context_var='ordering', sort_param='ordering', sort_field=None):
"""
A label to go in a table header cell, optionally with a 'sort' link that alternates between
forward and reverse sorting
label = label text
ordering = current active ordering. If not specified, we will fetch it from the template context variable
given by sort_context_var. (We don't fetch it from the URL because that wouldn't give the view method
the opportunity to set a default)
sort_param = URL parameter that indicates the current active ordering
sort_field = the value for sort_param that indicates that sorting is currently on this column.
For example, if sort_param='ordering' and sort_field='title', then a URL parameter of
ordering=title indicates that the listing is ordered forwards on this column, and a URL parameter
of ordering=-title indicated that the listing is ordered in reverse on this column
To disable sorting on this column, set sortable=False or leave sort_field unspecified.
"""
if not sortable or not sort_field:
# render label without a sort link
return label
if ordering is None:
ordering = context.get(sort_context_var)
reverse_sort_field = "-%s" % sort_field
if ordering == sort_field:
# currently ordering forwards on this column; link should change to reverse ordering
url = querystring(context, **{sort_param: reverse_sort_field})
classname = "icon icon-arrow-down-after teal"
elif ordering == reverse_sort_field:
# currently ordering backwards on this column; link should change to forward ordering
url = querystring(context, **{sort_param: sort_field})
classname = "icon icon-arrow-up-after teal"
else:
# not currently ordering on this column; link should change to forward ordering
url = querystring(context, **{sort_param: sort_field})
classname = "icon icon-arrow-down-after"
return format_html(
'<a href="{url}" class="{classname}">{label}</a>',
url=url, classname=classname, label=label
)
@register.simple_tag(takes_context=True)
def pagination_querystring(context, page_number, page_key=DEFAULT_PAGE_KEY):
"""