This commit is contained in:
Ross
2025-12-08 12:51:17 +00:00
parent 6f50c53519
commit 012c86340d
4 changed files with 64 additions and 52 deletions
+36
View File
@@ -483,6 +483,9 @@ class ExamSearchSelectMultipleWidget:
<ul id="{selected_list_id}" class="list-group list-group-flush">
{selected_html}
</ul>
<div class="mt-2 text-end">
<button type="button" id="exam_remove_all_{self.name}" class="btn btn-sm btn-outline-danger exam-remove-all-btn d-none" data-field="{self.name}">Remove all</button>
</div>
</div>
<script>
@@ -583,6 +586,15 @@ class ExamSearchSelectMultipleWidget:
}});
}}
// Wire up "Remove all" button for this widget instance
const removeAllBtn = document.getElementById('exam_remove_all_' + widgetFieldName);
if (removeAllBtn) {{
removeAllBtn.addEventListener('click', function(e) {{
e.preventDefault();
removeAllExamsFromField(widgetFieldName);
}});
}}
window.addExamToField = function(fieldName, id, text) {{
const sel = document.getElementById('id_' + fieldName);
if (!sel) return;
@@ -598,6 +610,9 @@ class ExamSearchSelectMultipleWidget:
const btn = document.createElement('button'); btn.type = 'button'; btn.className = 'btn btn-sm btn-outline-danger ms-2'; btn.innerText = 'Remove';
btn.addEventListener('click', function() {{ removeExamFromField(fieldName, id); }});
li.appendChild(span); li.appendChild(btn); listEl.appendChild(li);
// update the remove-all button visibility
var remBtn = document.getElementById('exam_remove_all_' + fieldName);
if (remBtn) {{ remBtn.classList.toggle('d-none', listEl.querySelectorAll('li').length < 2); }}
}}
window.removeExamFromField = function(fieldName, id) {{
@@ -606,6 +621,10 @@ class ExamSearchSelectMultipleWidget:
for (let i=sel.options.length-1;i>=0;i--) {{ if (sel.options[i].value == id) sel.remove(i); }}
const li = document.getElementById('selected_exam_' + fieldName + '_' + id);
if (li && li.parentNode) li.parentNode.removeChild(li);
// update the remove-all button visibility
var remBtn = document.getElementById('exam_remove_all_' + fieldName);
var listEl = document.getElementById('selected_exams_' + fieldName);
if (remBtn && listEl) {{ remBtn.classList.toggle('d-none', listEl.querySelectorAll('li').length < 2); }}
}}
window.addAllExamsFromResults = function(fieldName) {{
@@ -617,6 +636,23 @@ class ExamSearchSelectMultipleWidget:
const text = it.getAttribute('data-exam-text') || it.textContent.trim();
addExamToField(fieldName, id, text);
}});
// ensure remove-all button is visible if more than one
var remBtn = document.getElementById('exam_remove_all_' + fieldName);
var listEl = document.getElementById('selected_exams_' + fieldName);
if (remBtn && listEl) {{ remBtn.classList.toggle('d-none', listEl.querySelectorAll('li').length < 2); }}
}}
window.removeAllExamsFromField = function(fieldName) {{
const sel = document.getElementById('id_' + fieldName);
if (!sel) return;
// remove all options
for (let i=sel.options.length-1;i>=0;i--) {{ sel.remove(i); }}
// clear visible list
const listEl = document.getElementById('selected_exams_' + fieldName);
if (listEl) {{ listEl.innerHTML = ''; }}
// hide remove-all button
const remBtn = document.getElementById('exam_remove_all_' + fieldName);
if (remBtn) {{ remBtn.classList.add('d-none'); }}
}}
}})();
</script>