This repository contains a Stimulus controller for Symfony Autocomplete that adds "Select All" and "Unselect All" functionality to your autocomplete fields. This allows users to quickly select or deselect all available options in an autocomplete input.
- Select All: Automatically select all options in the autocomplete field.
- Unselect All: Clear all selected options.
- Responsive to changes: Buttons toggle based on the current selection.
- Customizable labels: Easily change button labels for select/deselect actions.
Before using this Stimulus controller, make sure you have the following packages installed in your Symfony project:
- Symfony (v5.2+)
- Symfony UX Autocomplete (Symfony UX)
- Stimulus (included with Symfony UX)
- TomSelect (for enhanced autocomplete functionality)
Copy the JavaScript code into your assets/controllers/form--autocomplete-select-all_controller.js file or wherever you manage your Stimulus controllers. autocomplete-select-all_controller.js
In your Symfony form type or HTML, add the following attributes to your autocomplete field:
'attr' => [
'data-controller' => 'form--autocomplete-select-all',
'data-label-select-all' => '<your label>', // Example: 'Select All'
'data-label-deselect-all' => '<your label>', // Example: 'Deselect All'
],
This will ensure that the controller attaches the select/unselect buttons to the autocomplete input.
If you're not using Bootstrap, you'll need to modify the button classes in the controller for custom styling.
Line 27 :
loadAllButton.classList.add('btn', 'btn-outline-primary', 'btn-sm', 'float-end', 'd-none', 'select-all-button')
Line 38 :
unselectAllButton.classList.add('btn', 'btn-outline-danger', 'btn-sm', 'float-end', 'd-none', 'unselect-all-button')
Modify this lines to fit your project's button styling classes.
Once integrated, users will be able to:
- Click "Select All": Automatically fetch and select all options from the server.
- Click "Deselect All": Clear all selected options.
- Buttons will toggle based on the current selection state, making it intuitive for users to manage large sets of options.
By following these steps, you can easily implement a select/unselect all feature in your Symfony project using Stimulus and Symfony UX Autocomplete. If you run into any issues or have suggestions for improvements, feel free to open an issue or submit a PR!
Enjoy coding! 😎