Stimulus Controller: PMACS Select All
The PmacsSelectAllController is a Stimulus controller that manages grouped checkbox selection and related bulk-action buttons.
When connected to a select-all checkbox (with data-pmacs-select-all-target="selectAllBox") and one or more item checkboxes (with data-pmacs-select-all-target="singleBox"), it will do the following:
- Select all
singleBoxcheckboxes when theselectAllBoxcheckbox is selected - Deselect all
singleBoxcheckboxes when theselectAllBoxcheckbox is deselected - Automatically maintain the correct state (checked or unchecked) of the
selectAllBoxcheckbox whenever the user selects asingleBox- If the user selects any
singleBoxcheckboxes, theselectAllBoxcheckbox will automatically be indeterminate - If the user selects all
singleBoxcheckboxes, theselectAllBoxcheckbox will automatically be checked
- If the user selects any
- Enable
buttontargets only when at least onesingleBoxcheckbox is checked
This can be used inside any element, including a table or a form.
Target Requirements
- Required:
singleBox,selectAllBox - Optional:
button
Using the PmacsSelectAllController
Stimulus controllers need to be connected to the DOM elements involved in the component. This is done by giving a DOM element a data-controller property with the name you have just registered: pmacs-select-all. The DOM element you choose needs to be high enough in the DOM hierarchy that all of the relevant checkboxes are nested within it, including the selectAllBox.
<div data-controller="pmacs-select-all"> <!-- All of your checkboxes must be nested inside here --> </div>
The selectAllBox target
Add the actual "Select All" checkbox to the DOM, which has the following properties:
<div> <input class="form-check-input" type="checkbox" value="" id="checkboxSelectAll" aria-label="Select/deselect all items" data-pmacs-select-all-target="selectAllBox" data-action="pmacs-select-all#toggleAllSingleBoxes" /> </div>
The singleBox target
All that remains is to connect the controller to the checkboxes. The following data-target and data-action properties are required:
<div> <input class="form-check-input" type="checkbox" value="" id="checkbox1" aria-label="Select/deselect item" data-pmacs-select-all-target="singleBox" data-action="pmacs-select-all#updateItems" /> </div>
The button target (optional)
If you want to disable your buttons until the checkboxes are checked, you can simply add data-pmacs-select-all-target="button", like so:
<button type="submit" disabled="disabled" data-pmacs-select-all-target="button"> Submit </button>
[!note]
We have disabled this button by default. This assumes that no checkboxes are initially checked.
Method Mapping
These are the controller methods you will typically wire through data-action:
toggleAllSingleBoxes: Called fromselectAllBoxto apply the main checked value to allsingleBoxtargets.updateItems: Called from eachsingleBoxto recalculate select-all checked/indeterminate state and button state.
Supporting methods run internally:
updateBulkActionBtns: Enables/disables allbuttontargets.handleMissingTargets: Validates required targets and applies fallback UI state when targets are missing.areAllBoxesCheckedandareAnyBoxesChecked: Helper checks used to compute state.
Troubleshooting
- Select all checkbox does not update items:
Verify
data-action="pmacs-select-all#toggleAllSingleBoxes"is set on theselectAllBoxinput. - Select all checkbox never updates its own state:
Verify each item checkbox has
data-action="pmacs-select-all#updateItems". - Buttons never enable:
Verify buttons include
data-pmacs-select-all-target="button"and at least onesingleBoxcan be checked. - Select all appears disabled:
This happens when
singleBoxtargets are missing or outside the controller root element. - Nothing works:
Verify all related elements are nested under the same
data-controller="pmacs-select-all"container.
That's It
All together, it should look something like this:
<div data-controller="pmacs-select-all"> <!-- Select-All Box --> <div class="form-check"> <input class="form-check-input" type="checkbox" value="" id="checkboxSelectAll" data-pmacs-select-all-target="selectAllBox" data-action="pmacs-select-all#toggleAllSingleBoxes" /> <label class="form-check-label" for="checkboxSelectAll"> Select All </label> </div> <!-- Checkbox 1 --> <div class="form-check"> <input class="form-check-input" type="checkbox" value="" id="checkbox1" data-pmacs-select-all-target="singleBox" data-action="pmacs-select-all#updateItems" /> <label class="form-check-label" for="checkbox1"> 1 </label> </div> <!-- Checkbox 2 --> <div class="form-check"> <input class="form-check-input" type="checkbox" value="" id="checkbox2" data-pmacs-select-all-target="singleBox" data-action="pmacs-select-all#updateItems" /> <label class="form-check-label" for="checkbox2"> 2 </label> </div> <!-- Submit Button --> <button type="submit" value="Submit" class="btn btn-primary" disabled="disabled" data-pmacs-select-all-target="button"> Submit </button> </div>