core/club: allow adding custom js action to pagination link, useful for FormDetailView with pagination

This commit is contained in:
Antoine Bartuccio 2019-11-28 01:15:57 +01:00
parent 8dd2c02d3e
commit 274a7b7137
Signed by: klmp200
GPG Key ID: E7245548C53F904B
3 changed files with 17 additions and 8 deletions

View File

@ -3,7 +3,7 @@
{% block content %}
<h3>{% trans %}Sellings{% endtrans %}</h3>
<form action="?page=1" method="post">
<form id="form" action="?page=1" method="post">
{% csrf_token %}
{{ form }}
<p><input type="submit" value="{% trans %}Show{% endtrans %}" /></p>
@ -28,7 +28,7 @@
</tr>
</thead>
<tbody>
{% for s in result %}
{% for s in paginated_result %}
<tr>
<td>{{ s.date|localtime|date(DATETIME_FORMAT) }} {{ s.date|localtime|time(DATETIME_FORMAT) }}</td>
<td>{{ s.counter }}</td>
@ -53,7 +53,14 @@
{% endfor %}
</tbody>
</table>
{{ paginate(result, paginator) }}
<script type="text/javascript">
function formPagination(link){
$("form").attr("action", link.href);
link.href = "javascript:void(0)"; // block link action
$("form").submit();
}
</script>
{{ paginate(paginated_result, paginator, "formPagination(this)") }}
{% endblock %}

View File

@ -354,6 +354,7 @@ class ClubSellingView(ClubTabsMixin, CanEditMixin, DetailFormView):
qs = Selling.objects.filter(club=self.object)
kwargs["result"] = qs[:0]
kwargs["paginated_result"] = kwargs["result"]
kwargs["total"] = 0
kwargs["total_quantity"] = 0
kwargs["benefit"] = 0
@ -392,7 +393,7 @@ class ClubSellingView(ClubTabsMixin, CanEditMixin, DetailFormView):
kwargs["paginator"] = Paginator(kwargs["result"], self.paginate_by)
try:
kwargs["result"] = kwargs["paginator"].page(self.asked_page)
kwargs["paginated_result"] = kwargs["paginator"].page(self.asked_page)
except InvalidPage:
raise Http404

View File

@ -113,10 +113,11 @@
{% endif %}
{% endmacro %}
{% macro paginate(page_obj, paginator) %}
{% macro paginate(page_obj, paginator, js_action) %}
{% set js = js_action|default('') %}
{% if page_obj.has_previous() or page_obj.has_next() %}
{% if page_obj.has_previous() %}
<a href="?page={{ page_obj.previous_page_number() }}">{% trans %}Previous{% endtrans %}</a>
<a {% if js %} type="submit" onclick="{{ js }}" {% endif %} href="?page={{ page_obj.previous_page_number() }}">{% trans %}Previous{% endtrans %}</a>
{% else %}
<span class="disabled">{% trans %}Previous{% endtrans %}</span>
{% endif %}
@ -124,11 +125,11 @@
{% if page_obj.number == i %}
<span class="active">{{ i }} <span class="sr-only">({% trans %}current{% endtrans %})</span></span>
{% else %}
<a href="?page={{ i }}">{{ i }}</a>
<a {% if js %} type="submit" onclick="{{ js }}" {% endif %} href="?page={{ i }}">{{ i }}</a>
{% endif %}
{% endfor %}
{% if page_obj.has_next() %}
<a href="?page={{ page_obj.next_page_number() }}">{% trans %}Next{% endtrans %}</a>
<a {% if js %} type="submit" onclick="{{ js }}" {% endif %} href="?page={{ page_obj.next_page_number() }}">{% trans %}Next{% endtrans %}</a>
{% else %}
<span class="disabled">{% trans %}Next{% endtrans %}</span>
{% endif %}