From 6293c560f750e8dc507f8f474cdc569328dd332d Mon Sep 17 00:00:00 2001
From: Jonathan Weth <git@jonathanweth.de>
Date: Wed, 17 Mar 2021 11:27:48 +0100
Subject: [PATCH] Support multiple tables/forms on one page

---
 aleksis/core/static/js/multi_select.js | 14 +++++++-------
 aleksis/core/util/tables.py            |  2 +-
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/aleksis/core/static/js/multi_select.js b/aleksis/core/static/js/multi_select.js
index e612f6eae..cddf911b5 100644
--- a/aleksis/core/static/js/multi_select.js
+++ b/aleksis/core/static/js/multi_select.js
@@ -1,17 +1,16 @@
 $(document).ready(function () {
-    $("#select_all_container").show();
-    $("#header_box").change(function () {
+    $(".select--header-box").change(function () {
         /*
         If the top checkbox is checked, all sub checkboxes should be checked,
         if it gets unchecked, all other ones should get unchecked.
         */
         if ($(this).is(":checked")) {
-            $(document).find('input[name="selected_objects"]').prop({
+            $(this).closest("table").find('input[name="selected_objects"]').prop({
                 indeterminate: false,
                 checked: true,
             });
         } else {
-            $(document).find('input[name="selected_objects"]').prop({
+            $(this).closest("table").find('input[name="selected_objects"]').prop({
                 indeterminate: false,
                 checked: false,
             });
@@ -27,10 +26,11 @@ $(document).ready(function () {
          */
         let checked = $(this).is(":checked");
         let indeterminate = false;
-        $(document).find('input[name="selected_objects"]').each(function () {
+        let table = $(this).closest("table");
+        table.find('input[name="selected_objects"]').each(function () {
             if ($(this).is(":checked") !== checked) {
                 /* Set the header box to indeterminate if the boxes are not the same */
-                $("#header_box").prop({
+                table.find(".select--header-box").prop({
                     indeterminate: true,
                 })
                 indeterminate = true;
@@ -39,7 +39,7 @@ $(document).ready(function () {
         });
         if (!(indeterminate)) {
             /* All boxes are the same, set the header box to the same value */
-            $("#header_box").prop({
+            table.find(".select--header-box").prop({
                 indeterminate: false,
                 checked: checked,
             });
diff --git a/aleksis/core/util/tables.py b/aleksis/core/util/tables.py
index 50a27a52b..8bb9fd5c0 100644
--- a/aleksis/core/util/tables.py
+++ b/aleksis/core/util/tables.py
@@ -40,7 +40,7 @@ class SelectColumn(MaterializeCheckboxColumn):
     def __init__(self, *args, **kwargs):
         kwargs["attrs"] = {
             "td__input": {"name": "selected_objects"},
-            "th__input": {"id": "header_box"},
+            "th__input": {"class": "select--header-box"},
         }
         kwargs.setdefault("accessor", A("pk"))
         super().__init__(*args, **kwargs)
-- 
GitLab