In this article we will try out a custom Coral UI Javascript in AEM and all a functionality to a dropdown where, dropdown will have n number of values and will have Select All and De-select All option as well in the dropdown. This is really helpful when you have lot’s of options in a dropdown and you have to select all and de-select all.
First create a simple AEM dropdown field with multiselect functionality and add some 5-6 options in the dropdown and then add two option with following text and value combination which will help us select all fields in dropdown as well as de-select all fields in dropdown.
NOTE: The name property used in this article is ./country for the dropdown.
Select All option:
text: Select All (You can keep any name as it just shows on the UI).
value: all (We are keeping all here because we will be using the same value in JS)
Deselect All option:
text: De-Select All (You can keep any name as it just shows on the UI).
value: none (We are keeping none here because we will be using the same value in JS)
Now create a clientlibrary in AEM with category name as cq.myproject.select-deselect. And then attach this clientlib at the cq:dialog node of the component by adding the following property at cq:dialog node.
extraClientlibs – String – cq.myproject.select-deselect
Once we are done setting up everything then paste the following code in your JS file of the clientlibs and checkout the functionality in the dropdown.
(function (document, $){
"use strict";
$(document).on("foundation-field-change", "coral-select[name='./country']", function (e) {
let postSelect = $("coral-tag[aria-selected='true']").val();
let allDropdownselect=($(this).Find("coral-selectlist-item(value='all']")).is(':selected');
let nodropdownSelect = ($(this).find("coral-selectlist-item[value='none']")).is(':selected');
if (!!allDropdownselect && !postSelect) {
$(this) .find("coral-selectlist-item[value! ='none']").attr("selected", true);
$(this). find("coral-tag[value='all']"). remove();
}
if (!!nodropdownSelect && !postSelect) {
$(this).find("coral-selectlist-item[value!='none']").attr("selected", false);
$(this).find("coral-tag[value='none' ]") .remove();
}
});
}) (document, Granite.$);
Also if you want to customise the above Coral UI JS as per your name property and dropdown options then you need to update the following props.
Field Name | Purpose |
---|---|
./country | Used as the name property of the dropdown field |
all | Used as the value of Select All dropdown field |
none | Used as the value of De-Select All dropdown field |