How to show/hide dialog fields with a checkbox in AEM Touch UI

2.3k Views Asked by At

I am relatively new to AEM and I am trying to hide/show dialog fields on checkbox click. I have tried some ways but failed to achieve this functionality. This is just for my own learning. How can I achieve this?

I have tried adding the js clientlib and adding some classes and target to the checkbox and target fields respectively as suggested in other answers but it didn't seem to work. Please help.

1

There are 1 best solutions below

2
Pakira On

First you need to create a clientLibs and add categories as cq.authoring.dialog.all, see the code below:

  (function($, $document) {
 $document.on("dialog-ready", function() {
   Coral.commons.ready($document, function () {   
  dispalyOrHideTabs();   
  $(document).on('change', '#showText', function() {

      if($('#showText').attr('checked')){
        show('1');
      }else{
          hide('1');    
      }

 });

 $(document).on('change', '#showTable', function() {

       if($('#showTable').attr('checked')){
          show('2');
      }else{
          hide('2');    
      }

 });    

     function hide(index){
                     var tab = $document.find("[id='compareImgId-"+index+"']").closest(".coral3-Panel");
                      var tab2 = tab.attr("aria-labelledby");
                      var tab3 = $document.find("[id='"+tab2+"']");
                      tab3.addClass("hide");
     }
     function show(index){
                     var tab = $document.find("[id='compareImgId-"+index+"']").closest(".coral3-Panel");
                      var tab2 = tab.attr("aria-labelledby");
                      var tab3 = $document.find("[id='"+tab2+"']");
                      tab3.removeClass("hide");
     }

     function dispalyOrHideTabs(){
         var editable = Granite.author.DialogFrame.currentDialog.editable;
         if(editable){
        var node = Granite.HTTP.eval(Granite.author.DialogFrame.currentDialog.editable.path + ".json");
         if(node){
            var storedTextValue = node.showText;
             var storedTableValue = node.showTable;

             if(storedTextValue){
                    show('1');
             }else{
                    hide('1');
             }

             if(storedTableValue){
                    show('2');
             }else{
                    hide('2');
             }

         }
     }

     }

    });
     });

  })($, $(document));

Add granite:id property as showText of the checkbox resource type.

And below is the dialog tabs which will be hidden and shown: enter image description here