From ac5041f3ec2bc0d84574fdbdce4adccce58aa6f8 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sun, 21 Jun 2020 22:27:32 +0200 Subject: [PATCH] Better club search bar --- apps/member/tables.py | 3 +- apps/member/views.py | 16 +++++++++ templates/member/club_list.html | 59 ++++++++++++++------------------- templates/member/user_list.html | 14 +++++--- 4 files changed, 51 insertions(+), 41 deletions(-) diff --git a/apps/member/tables.py b/apps/member/tables.py index 515d7836..8f5ceb88 100644 --- a/apps/member/tables.py +++ b/apps/member/tables.py @@ -24,7 +24,8 @@ class ClubTable(tables.Table): } model = Club template_name = 'django_tables2/bootstrap4.html' - fields = ('id', 'name', 'email') + fields = ('name', 'email',) + order_by = ('name',) row_attrs = { 'class': 'table-row', 'id': lambda record: "row-" + str(record.pk), diff --git a/apps/member/views.py b/apps/member/views.py index 04742f32..c52522e2 100644 --- a/apps/member/views.py +++ b/apps/member/views.py @@ -299,6 +299,22 @@ class ClubListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView): model = Club table_class = ClubTable + def get_queryset(self, **kwargs): + """ + Filter the user list with the given pattern. + """ + qs = super().get_queryset().filter() + if "search" in self.request.GET: + pattern = self.request.GET["search"] + + qs = qs.filter( + Q(name__iregex=pattern) + | Q(note__alias__name__iregex="^" + pattern) + | Q(note__alias__normalized_name__iregex=Alias.normalize("^" + pattern)) + ) + + return qs + class ClubDetailView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView): """ diff --git a/templates/member/club_list.html b/templates/member/club_list.html index 8ba0ef3b..b43a186c 100644 --- a/templates/member/club_list.html +++ b/templates/member/club_list.html @@ -7,7 +7,7 @@

{% trans "search clubs" %}

- +
{% trans "Create club" %} @@ -28,43 +28,32 @@ {% endblock %} {% block extrajavascript %} {% endblock %} diff --git a/templates/member/user_list.html b/templates/member/user_list.html index 018c479d..d7628882 100644 --- a/templates/member/user_list.html +++ b/templates/member/user_list.html @@ -25,6 +25,8 @@ $(document).ready(function() { let old_pattern = null; let searchbar_obj = $("#searchbar"); + var timer_on = false; + var timer; function reloadTable() { let pattern = searchbar_obj.val(); @@ -33,17 +35,19 @@ return; $("#user_table").load(location.href + "?search=" + pattern.replace(" ", "%20") + " #user_table", init); - - $(".table-row").click(function() { - window.document.location = $(this).data("href"); - }); } - searchbar_obj.keyup(reloadTable); + searchbar_obj.keyup(function() { + if (timer_on) + clearTimeout(timer); + timer_on = true; + setTimeout(reloadTable, 0); + }); function init() { $(".table-row").click(function() { window.document.location = $(this).data("href"); + timer_on = false; }); }