AEM: Hide/Show Touch UI dialog fields based on radio selection

  Hello, I observed a lot of people asking the same question or similar kind of, How to Hide/Show field based on selection.

In my previous post we have seen how to achieve the same thing on checkbox selection. Check the below link.

See how to Hide/Show Touch UI dialog fields based on checkbox selection 

Now we will see how to achieve the same thing using radio button.

Let's get started

In order to achieve this we need to create our own custom client library. I suggest not to use OOTB categories. To know how to create custom client library and configure it in dialog check out below link. Ignore if you already know.

See how to Create custom Touch Dialog UI edit client libraries

Now we know how to create the custom client library and we created and configured in our dialog.

Here I have a Demo Component and in the dialog I have 3 fields as show in below image.

demo-dialog

Here I have two text fields and one radio group. Let's see how to hide/show the text field based on radio selection.

Below is the node and properties of radio group.

radio-group

We have created listerner.js file as part of our custom client library, let's open that.

listener.js

Let's add a self executable function, inside that add dialog ready event.

(function ($, document, ns) {

    $(document).on("dialog-ready", function() {

    });

})(Granite.$, document, Granite.author);

 The dialog ready event listener(or call back) will be fired once dialog is ready.

Now let's write a change event listener for radio buttons in side dialog ready.

(function ($, document, ns) {
    $(document).on("dialog-ready", function() {
const showHideSubTitle = function(e) {
            if($("[name='./hide-subtitle']:checked").val() == "hide") {
                $("input[name='./sub-title']").closest(".coral-Form-fieldwrapper").hide();
            } else {
                $("input[name='./sub-title']").closest(".coral-Form-fieldwrapper").show();
            }
        };
$(document).on("change", "[name='./hide-subtitle']", showHideSubTitle);
showHideSubTitle();
    });
})(Granite.$, document, Granite.author);

Note: The highlighted name should be updated. 

Now Save the changes and hard reload the page and open the dialog.

Change the radio options and you can see the sub-title field is hiding and showing upon selection.

dialog-final


Hurrah.... We have successfully achieved  the hide and show functionality based on radio button selection.

Hope you liked the post and it helped you. Meet you in next post. Thank you very much for your time.

-AG  

No comments:

Post a Comment