Allow custom target for htmx pagination

This commit is contained in:
2026-03-30 19:43:22 +02:00
parent b5d8db0187
commit 74a089a529
2 changed files with 57 additions and 55 deletions

View File

@@ -22,14 +22,10 @@
{% from "core/macros.jinja" import paginate_htmx %} {% from "core/macros.jinja" import paginate_htmx %}
{% block content %} {% block content %}
{% if not is_fragment %}
<main> <main>
<h3>{% trans %}Filters{% endtrans %}</h3> <h3>{% trans %}Filters{% endtrans %}</h3>
<form <form>
id="club-list-filters"
hx-get="{{ url("club:club_list") }}"
hx-target="#content"
hx-swap="outerHtml"
>
<div class="row gap-4x"> <div class="row gap-4x">
{{ form }} {{ form }}
</div> </div>
@@ -44,6 +40,8 @@
<i class="fa fa-plus"></i> {% trans %}New club{% endtrans %} <i class="fa fa-plus"></i> {% trans %}New club{% endtrans %}
</a> </a>
{% endif %} {% endif %}
<div id="paginated">
{% endif %}
<section class="aria-busy-grow" id="club-list"> <section class="aria-busy-grow" id="club-list">
{% for club in object_list %} {% for club in object_list %}
<div class="card"> <div class="card">
@@ -65,11 +63,14 @@
</div> </div>
</div> </div>
{% endfor %} {% endfor %}
</section>
{% if is_paginated %} {% if is_paginated %}
{{ paginate_htmx(request, page_obj, paginator) }} {{ paginate_htmx(request, page_obj, paginator, htmx_target="#paginated") }}
{% endif %} {% endif %}
</section>
{% if not is_fragment %}
</div>
</main> </main>
{% endif %}
{% endblock %} {% endblock %}

View File

@@ -129,10 +129,10 @@
current_page (django.core.paginator.Page): the current page object current_page (django.core.paginator.Page): the current page object
paginator (django.core.paginator.Paginator): the paginator object paginator (django.core.paginator.Paginator): the paginator object
#} #}
{{ paginate_server_side(request, current_page, paginator, False) }} {{ paginate_server_side(request, current_page, paginator, "") }}
{% endmacro %} {% endmacro %}
{% macro paginate_htmx(request, current_page, paginator) %} {% macro paginate_htmx(request, current_page, paginator, htmx_target="#content") %}
{# Add pagination buttons for pages without Alpine but supporting fragments. {# Add pagination buttons for pages without Alpine but supporting fragments.
This must be coupled with a view that handles pagination This must be coupled with a view that handles pagination
@@ -144,18 +144,19 @@
request (django.http.request.HttpRequest): the current django request request (django.http.request.HttpRequest): the current django request
current_page (django.core.paginator.Page): the current page object current_page (django.core.paginator.Page): the current page object
paginator (django.core.paginator.Paginator): the paginator object paginator (django.core.paginator.Paginator): the paginator object
htmx_target (string): htmx target selector (default '#content')
#} #}
{{ paginate_server_side(request, current_page, paginator, True) }} {{ paginate_server_side(request, current_page, paginator, htmx_target) }}
{% endmacro %} {% endmacro %}
{% macro paginate_server_side(request, current_page, paginator, use_htmx) %} {% macro paginate_server_side(request, current_page, paginator, htmx_target) %}
<nav class="pagination"> <nav class="pagination">
{% if current_page.has_previous() %} {% if current_page.has_previous() %}
<a <a
{% if use_htmx -%} {% if htmx_target -%}
hx-get="?{{ querystring(request, page=current_page.previous_page_number()) }}" hx-get="?{{ querystring(request, page=current_page.previous_page_number()) }}"
hx-swap="innerHTML" hx-swap="innerHTML"
hx-target="#content" hx-target="{{ htmx_target }}"
hx-push-url="true" hx-push-url="true"
hx-trigger="click, keyup[key=='ArrowLeft'] from:body" hx-trigger="click, keyup[key=='ArrowLeft'] from:body"
{%- else -%} {%- else -%}
@@ -176,10 +177,10 @@
<strong>{{ paginator.ELLIPSIS }}</strong> <strong>{{ paginator.ELLIPSIS }}</strong>
{% else %} {% else %}
<a <a
{% if use_htmx -%} {% if htmx_target -%}
hx-get="?{{ querystring(request, page=i) }}" hx-get="?{{ querystring(request, page=i) }}"
hx-swap="innerHTML" hx-swap="innerHTML"
hx-target="#content" hx-target="{{ htmx_target }}"
hx-push-url="true" hx-push-url="true"
{%- else -%} {%- else -%}
href="?{{ querystring(request, page=i) }}" href="?{{ querystring(request, page=i) }}"
@@ -191,10 +192,10 @@
{% endfor %} {% endfor %}
{% if current_page.has_next() %} {% if current_page.has_next() %}
<a <a
{% if use_htmx -%} {% if htmx_target -%}
hx-get="?{{querystring(request, page=current_page.next_page_number())}}" hx-get="?{{querystring(request, page=current_page.next_page_number())}}"
hx-swap="innerHTML" hx-swap="innerHTML"
hx-target="#content" hx-target="{{ htmx_target }}"
hx-push-url="true" hx-push-url="true"
hx-trigger="click, keyup[key=='ArrowRight'] from:body" hx-trigger="click, keyup[key=='ArrowRight'] from:body"
{%- else -%} {%- else -%}