-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade check_all widget to component #166
- Loading branch information
Showing
2 changed files
with
47 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// Deprecated. Use select_all.html instead. | ||
|
||
$("#checkall").click(function () { | ||
$('input:checkbox').not(this).prop('checked', this.checked); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{% comment %} | ||
Easily select or deselect all the checkboxes for a given field. | ||
|
||
Make sure to wrap this widget and the checkboxes in fieldset. | ||
|
||
Usage: | ||
|
||
<fieldset> | ||
<legend>Which colours?</legend> | ||
{% include "admin/core/widgets/select_all.html" %} | ||
<input id="red" name="red" type="checkbox"><label for="red">Red</label> | ||
<input id="blue" name="blue" type="checkbox"><label for="blue">Blue</label> | ||
</fieldset> | ||
|
||
{% endcomment %} | ||
|
||
{% load uuid %} | ||
|
||
{% short_uuid4 as pid %} | ||
|
||
<div id="{{ pid }}" style="button-group"> | ||
<button class="button selectall" type="button"> | ||
<span class="fa fa-check"></span> | ||
Select all | ||
</button> | ||
<button class="button deselectall" type="button"> | ||
<span class="fa fa-close"></span> | ||
Deselect all | ||
</button> | ||
</div> | ||
<script defer type="module"> | ||
function toggleInputsInFieldset(event) { | ||
const checked = event.currentTarget.classList.contains('selectall'); | ||
const fieldset = event.currentTarget.closest('fieldset'); | ||
const checkboxes = fieldset.querySelectorAll('input[type="checkbox"]'); | ||
Array.from(checkboxes).forEach(checkbox => { | ||
checkbox.checked = checked; | ||
}); | ||
} | ||
const selectAll = document.querySelector('#{{ pid }} .selectall'); | ||
selectAll.addEventListener('click', toggleInputsInFieldset); | ||
const deselectAll = document.querySelector('#{{ pid }} .deselectall'); | ||
deselectAll.addEventListener('click', toggleInputsInFieldset); | ||
</script> |