#151 Parcel table infinite scroll

* refactors button for further loading to infinite scroll
* adds code documentation
This commit is contained in:
mpeltriaux 2022-04-21 14:36:55 +02:00
parent 2dff3cfce3
commit 376a32182b
3 changed files with 38 additions and 25 deletions

View File

@ -1,5 +1,16 @@
{% load l10n i18n %} {% load l10n i18n %}
{% for parcel in parcels %} {% for parcel in parcels %}
{% if forloop.last and next_page %}
<tr hx-get="{% url 'geometry-parcels-content' geom_id next_page %}"
hx-trigger="intersect once"
hx-swap="afterend">
<td>{{parcel.parcel_group.name|default_if_none:"-"}}</td>
<td>{{parcel.parcel_group.key|default_if_none:"-"}}</td>
<td>{{parcel.flr|default_if_none:"-"|unlocalize}}</td>
<td>{{parcel.flrstck_zhlr|default_if_none:"-"|unlocalize}}</td>
<td>{{parcel.flrstck_nnr|default_if_none:"-"|unlocalize}}</td>
</tr>
{% else %}
<tr> <tr>
<td>{{parcel.parcel_group.name|default_if_none:"-"}}</td> <td>{{parcel.parcel_group.name|default_if_none:"-"}}</td>
<td>{{parcel.parcel_group.key|default_if_none:"-"}}</td> <td>{{parcel.parcel_group.key|default_if_none:"-"}}</td>
@ -7,16 +18,5 @@
<td>{{parcel.flrstck_zhlr|default_if_none:"-"|unlocalize}}</td> <td>{{parcel.flrstck_zhlr|default_if_none:"-"|unlocalize}}</td>
<td>{{parcel.flrstck_nnr|default_if_none:"-"|unlocalize}}</td> <td>{{parcel.flrstck_nnr|default_if_none:"-"|unlocalize}}</td>
</tr> </tr>
{% endif %}
{% endfor %} {% endfor %}
{% if next_page %}
<tr id="nextButton">
<td class="text-center" colspan="5">
<button class="btn btn-default w-50"
hx-get="{% url 'geometry-parcels-content' geom_id next_page %}"
hx-target="#nextButton"
hx-swap="outerHTML">
{% trans 'Show more...' %}
</button>
</td>
</tr>
{% endif %}

View File

@ -36,7 +36,7 @@
<th scope="col">{% trans 'Parcel number' %}</th> <th scope="col">{% trans 'Parcel number' %}</th>
</tr> </tr>
</thead> </thead>
<tbody class="m5"> <tbody>
{% include 'konova/includes/parcels/parcel_table_content.html' %} {% include 'konova/includes/parcels/parcel_table_content.html' %}
</tbody> </tbody>
</table> </table>

View File

@ -110,7 +110,7 @@ def get_geom_parcels(request: HttpRequest, id: str):
id (str): The geometry's id id (str): The geometry's id
Returns: Returns:
A rendered piece of HTML
""" """
# HTTP code 286 states that the HTMX should stop polling for updates # HTTP code 286 states that the HTMX should stop polling for updates
# https://htmx.org/docs/#polling # https://htmx.org/docs/#polling
@ -134,7 +134,7 @@ def get_geom_parcels(request: HttpRequest, id: str):
municipals = parcels.order_by("municipal").distinct("municipal").values("municipal__id") municipals = parcels.order_by("municipal").distinct("municipal").values("municipal__id")
municipals = Municipal.objects.filter(id__in=municipals) municipals = Municipal.objects.filter(id__in=municipals)
rpp = 50 rpp = 100
parcels = parcels[:rpp] parcels = parcels[:rpp]
next_page = 1 next_page = 1
if len(parcels) < rpp: if len(parcels) < rpp:
@ -153,7 +153,20 @@ def get_geom_parcels(request: HttpRequest, id: str):
@login_required @login_required
def get_geom_parcels_content(request:HttpRequest, id: str, page: int): def get_geom_parcels_content(request: HttpRequest, id: str, page: int):
""" Getter for infinite scroll of HTMX
Returns parcels of a specific page/slice of the found parcel set.
Implementation of infinite scroll htmx example: https://htmx.org/examples/infinite-scroll/
Args:
request (HttpRequest): The incoming request
id (str): The geometry's id
page (int): The requested page number
Returns:
A rendered piece of HTML
"""
if page < 0: if page < 0:
raise AssertionError("Parcel page can not be negative") raise AssertionError("Parcel page can not be negative")
@ -165,7 +178,7 @@ def get_geom_parcels_content(request:HttpRequest, id: str, page: int):
parcels = geom.get_underlying_parcels() parcels = geom.get_underlying_parcels()
parcels = parcels.order_by("-municipal", "flr", "flrstck_zhlr", "flrstck_nnr") parcels = parcels.order_by("-municipal", "flr", "flrstck_zhlr", "flrstck_nnr")
rpp = 50 rpp = 100
from_p = rpp * (page-1) from_p = rpp * (page-1)
to_p = rpp * (page) to_p = rpp * (page)
next_page = page + 1 next_page = page + 1