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

Add django-tables2 and utils, example, docs for usage with HTMX #35470

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
bc2bbd5
add django_tables2
biyeun Nov 19, 2024
6c3991e
add render_header template tag (to hq_tables_tags)
biyeun Nov 19, 2024
ce5dc40
add base bootstrap5 and bootstrap5 (with HTMX) templates for django t…
biyeun Nov 19, 2024
7739c24
add saved, selectable pagination support to django tables
biyeun Nov 19, 2024
89a8925
add styling for sortable headers with django tables
biyeun Nov 20, 2024
cbf36e8
trigger additional custom loading states with hq-hx-loading
biyeun Nov 20, 2024
8b89672
add the hq-hx-refresh HTMX plugin
biyeun Nov 21, 2024
0d6eeed
update hq_hx_refresh to be a little clearer
biyeun Dec 3, 2024
c22f690
add hq-hx loading and refresh plugins to base HTMX/Alpine entry point
biyeun Dec 3, 2024
8462408
fix spacing
biyeun Dec 3, 2024
798e0bb
add more fake data generators
biyeun Dec 3, 2024
9ac7666
add BaseHtmxTable class
biyeun Dec 3, 2024
0ca8a1d
css_id should be container_id
biyeun Dec 3, 2024
ff017a7
make sure clicking on table headers triggers the loading indicator fo…
biyeun Dec 3, 2024
0813758
update util to use fake_data from prototype
biyeun Dec 3, 2024
5a80d6a
add fake data for pagination example
biyeun Dec 3, 2024
eb3e897
add pagination example and documentation for HTMX + django-tables2
biyeun Dec 3, 2024
09338eb
update docs for htmx and alpine
biyeun Dec 3, 2024
f9c29d7
"Bootstrap 5 Migration - Rebuilt diffs"
biyeun Dec 9, 2024
615aaea
cleanup column sorting variables in render_header
biyeun Dec 17, 2024
0161947
make sure to include a space between first and last names
biyeun Dec 17, 2024
24853e4
simplify -- address PR feedback
biyeun Dec 17, 2024
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
Empty file.
52 changes: 52 additions & 0 deletions corehq/apps/hqwebapp/tables/pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from django.core.paginator import Paginator
from django.views.generic.list import ListView

from django_tables2 import SingleTableMixin


class SelectablePaginator(Paginator):
paging_options = [10, 25, 50, 100]
default_option = 25


class SelectablePaginatedTableMixin(SingleTableMixin):
"""
Use this mixin with django-tables2's SingleTableView

Specify a `urlname` attribute to assist with naming the pagination cookie,
otherwise the cookie slug will default to using the class name.
"""
# `paginator_class` should always be a subclass of `SelectablePaginator`
paginator_class = SelectablePaginator

@property
def paginate_by_cookie_slug(self):
slug = self.urlname if hasattr(self, "urlname") else self.__class__.__name__
biyeun marked this conversation as resolved.
Show resolved Hide resolved
return f'{slug}-paginate_by'

@property
def default_paginate_by(self):
return self.request.COOKIES.get(
self.paginate_by_cookie_slug,
self.paginator_class.default_option
)

@property
def current_paginate_by(self):
return self.request.GET.get('per_page', self.default_paginate_by)

def get_paginate_by(self, table_data):
return self.current_paginate_by


class SelectablePaginatedTableView(SelectablePaginatedTableMixin, ListView):
"""
Based on SingleTableView, which inherits from `SingleTableMixin`, `ListView`
we instead extend the `SingleTableMixin` with `SavedPaginatedTableMixin`.
"""
template_name = "hqwebapp/tables/single_table.html"

def get(self, request, *args, **kwargs):
response = super().get(request, *args, **kwargs)
response.set_cookie(self.paginate_by_cookie_slug, self.current_paginate_by)
return response
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{% load render_table from django_tables2 %}
{% render_table table %}