Skip to content

Commit c20da0b

Browse files
authored
Added multipage selection for checkboxes on admin pages (#1555) (#1609)
1 parent bf87c3d commit c20da0b

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

templates/admin/change_list.html

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
{% extends "admin/change_list.html" %}
2+
{% block extrahead %}
3+
{{ block.super }}
4+
<script>
5+
// if you change this, change delete_selected_confirmation.html too to match
6+
const storageKey = `selected_admin_ids-${window.location.pathname.toString().slice(1, -1).replace(/\//g,"_")}`;
7+
8+
const getSelectedIds = () => {
9+
const stored = localStorage.getItem(storageKey);
10+
return stored ? JSON.parse(stored) : [];
11+
}
12+
13+
const setSelectedIds = (ids) => {
14+
localStorage.setItem(storageKey, JSON.stringify(ids));
15+
}
16+
17+
const addId = (id) => {
18+
let selected = getSelectedIds();
19+
if (!selected.includes(id)) {
20+
selected.push(id);
21+
setSelectedIds(selected);
22+
}
23+
}
24+
25+
const removeId = (id) => {
26+
const selected = getSelectedIds();
27+
setSelectedIds(selected.filter(item => item !== id));
28+
}
29+
30+
const clearIds = () => {
31+
const action_selects = document.querySelectorAll("input[name=_selected_action]:checked");
32+
for (let i = 0; i < action_selects.length; i++) {
33+
action_selects[i].checked = false;
34+
}
35+
setSelectedIds([]);
36+
setTimeout(updateActionCounter, 1);
37+
}
38+
39+
const updateActionCounter = () => {
40+
const selectedIds = getSelectedIds();
41+
const checkedCheckboxes = document.querySelectorAll("input[name=_selected_action]:checked");
42+
const counter = document.querySelector(".action-counter");
43+
44+
if (counter) {
45+
let actionCounterContent = "Selected - Page: 0, Total: 0";
46+
if (checkedCheckboxes.length && selectedIds.length) {
47+
actionCounterContent = counter.textContent.replace(
48+
/([\W\w]+) selected([\W\w]?)/,
49+
`Selected - Page: ${checkedCheckboxes.length || 0}, Total: ${selectedIds.length || 0} ❌`
50+
);
51+
}
52+
counter.textContent = actionCounterContent;
53+
counter.title = "Click to clear all";
54+
}
55+
}
56+
57+
document.addEventListener("DOMContentLoaded", () => {
58+
const checkboxes = document.querySelectorAll("input.action-select");
59+
const selectedIds = getSelectedIds();
60+
setTimeout(updateActionCounter, 1);
61+
const counter = document.querySelector(".action-counter");
62+
counter.addEventListener("click", () => {
63+
if (window.confirm("Are you sure you want to clear all selected items across all pages for this form?")) {
64+
clearIds();
65+
}
66+
});
67+
68+
checkboxes.forEach((checkbox) => {
69+
if (selectedIds.includes(checkbox.value)) {
70+
checkbox.checked = true;
71+
}
72+
73+
checkbox.addEventListener("change", () => {
74+
if (checkbox.checked) {
75+
addId(checkbox.value);
76+
} else {
77+
removeId(checkbox.value);
78+
}
79+
setTimeout(updateActionCounter, 1);
80+
});
81+
});
82+
83+
const form = document.querySelector("form#changelist-form");
84+
if (form) {
85+
form.addEventListener("submit", (event) => {
86+
const selected = getSelectedIds();
87+
selected.forEach((id) => {
88+
const hiddenInput = document.createElement("input");
89+
hiddenInput.type = "hidden";
90+
hiddenInput.name = "_selected_action";
91+
hiddenInput.value = id;
92+
form.appendChild(hiddenInput);
93+
});
94+
});
95+
}
96+
});
97+
98+
</script>
99+
{% endblock %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{% extends "admin/delete_selected_confirmation.html" %}
2+
3+
{% block extrahead %}
4+
{{ block.super }}
5+
<script>
6+
document.addEventListener("DOMContentLoaded", () => {
7+
// if you change this, change change_list.html too to match
8+
const storageKey = `selected_admin_ids-${window.location.pathname.toString().slice(1, -1).replace(/\//g,"_")}`;
9+
const form = document.querySelector("form");
10+
if (form) {
11+
form.addEventListener("submit", () => {
12+
localStorage.removeItem(storageKey);
13+
});
14+
}
15+
});
16+
</script>
17+
{% endblock %}

0 commit comments

Comments
 (0)