Showing posts with label dropdown. Show all posts
Showing posts with label dropdown. Show all posts

AEM: Show/Hide Dialog Tab based on selection(checkbox, radio, select)

 Hello, Let's see how to Show/Hide coral3 Touch UI Dialog Tab based on a selection(checkbox, radio, select).

Let's get started

To achieve this we need to create a 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, configured in our dialog.

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

demo-dialog

I have added id to each of the tab as shown in below image to identify/get the tab in JavaScript.

tab-node


Show/Hide Tab on Checkbox selection

Here we have 2 text fields and a checkbox. Let us see to to show/hide the Tab Two based on checkbox(Hide Tab Two) selection.

Below is the node and properties for checkbox.

checkbox-node

Note:  It is recommended to set value, uncheckedValue properties. Check below documentation for checkbox.

https://helpx.adobe.com/experience-manager/6-5/sites/developing/using/reference-materials/granite-ui/api/jcr_root/libs/granite/ui/components/coral/foundation/form/checkbox/index.html

We have listener.js file in our custom client library that we created, let us 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 checkbox in side dialog ready.

(function ($, document, ns) {
    $(document).on("dialog-ready", function() {
const showHideTab = function(e) {
            const tabTwo = $(".coral3-Tab[aria-controls='"+$("#tab-two").closest(".coral3-Panel").attr("id")+"']");
            if($("[name='./hide-tab']:checked").val() == "hide") {
                tabTwo.hide();
            } else {
                tabTwo.show();
            }
        };
$(document).on("change", "[name='./hide-tab']", showHideTab);
showHideTab();
    });
})(Granite.$, document, Granite.author);

Note: The highlighted name should be updated

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

Check and un-check the checkbox and you can see the Tab Two is hiding and showing upon selection.

final-dialog-checkbox


Show/Hide Tab on Radio button selection

Here I have two text fields, radio group and two tabs. Let's see how to hide/show Tab based on radio selection.

Below is the node and properties of radio group.

radiogroup-node

Now update the listener.js.

listener.js

(function ($, document, ns) {
    $(document).on("dialog-ready", function() {
const showHideTab = function(e) {
            const tabTwo = $(".coral3-Tab[aria-controls='"+$("#tab-two").closest(".coral3-Panel").attr("id")+"']");
            if($("[name='./hide-tabtwo']:checked").val() == "hide") {
                tabTwo.hide();
            } else {
                tabTwo.show();
            }
        };
$(document).on("change", "[name='./hide-tabtwo']", showHideTab);
showHideTab();
    });
})(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 Tab Two is hiding and showing upon selection.

final-dialog-radio


Show/Hide Tab on Dropdown(select) selection

Here I have two text fields, dropdown and two tabs. Let's see how to hide/show Tab based on dropdown selection.

dialog-select


Below is the node and properties of dropdown.

select-node

Now update the listener.js.

listener.js

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

        const showHideTab = function () {
            const tabTwo = $(".coral3-Tab[aria-controls='"+$("#tab-two").closest(".coral3-Panel").attr("id")+"']");
            if ($(".cq-dialog").find("#hidetab")[0].selectedItem.value === "hide") {
tabTwo.hide();
            } else {
tabTwo.show();
            }
        };


       $(".cq-dialog").find("#hidetab").on("change", showHideTab);
        showHideTab();
    });
})(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 dropdown options and you can see the Tab Two is hiding and showing upon selection.

final-dialog-select

Hurrah.... We have successfully achieved  the hide and show functionality based on selection of checkbox, radio and select options.

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

-AG  

AEM: Create Coral3 Touch UI dropdown from JSON file (JSP)


Hello, Let us see how to create a dropdown values using JSON file which is inside DAM. I am going to explain how to achieve this using JSP file.


Use case:

In some cases authors or business might want to add or remove values from dropdown of a authoring dialog. A JSON file inside DAM will be easy for authors to update whenever required without a Developer intervention. 


Why JSP?

  • We know that the JSP is internally a Servlet. 
  • As JSP file will be as part of our component code base, it is very is easy to update. 
  • The usage of this JSP file is only as part Dialog/Edit mode.

Implementation:

I have created a Demo Component which is having a Language dropdown inside authoring dialog.


initial-dialog

Below is the node structure.


select-node

Now instead of creating options nodes under items, we will create a data source node under the coral select field.

  • Create a node under select field with name datasource type nt:unstructured
  • Add sling:resourceType property and give datasource(JSP) path (yet to create)

datasource-node

Now let us create a folder(nt:folder) with same path as we configured in datasource node. Create a file(language.jsp) under the folder.

Here I have created a folder under my project/component folder with name datasource and inside that I have created a folder with name language similar to the path I have specified in datasource node.

Under language folder I have created a jsp file with name language.jsp (jsp file name and folder name must be same).

datasource-file

Now let us create and upload a JSON file in DAM

 Here in this example I have created a JSON file(language.json) with different languages like below and uploaded in DAM.

[
  {
    "value": "arabic",
    "text": "Arabic"
  },
  {
    "value": "chinese",
    "text": "Chinese"
  },
  {
    "value": "dutch",
    "text": "Dutch"
  },
  {
    "value": "english",
    "text": "English"
  },
  {
    "value": "french",
    "text": "French"
  },
  {
    "value": "german",
    "text": "German"
  },
  {
    "value": "italian",
    "text": "Italian"
  },
  {
    "value": "japanese",
    "text": "Japanese"
  },
  {
    "value": "russian",
    "text": "Russian"
  }
]

Logic to read JSON file and add options to select(dropdown)

Now inside the jsp file add the below code to read JSON file and add options to select field.

<%@page session="false" 
import="org.apache.sling.api.resource.Resource,
        org.apache.sling.api.resource.ValueMap,
        org.apache.sling.api.resource.ResourceMetadata,
        org.apache.sling.api.wrappers.ValueMapDecorator,
    org.json.simple.parser.JSONParser,
        com.adobe.granite.ui.components.ds.DataSource,
        com.adobe.granite.ui.components.ds.SimpleDataSource,
        com.adobe.granite.ui.components.ds.ValueMapResource"%>

<%@taglib prefix="cq" uri="http://www.day.com/taglibs/cq/1.0" %>
<cq:defineObjects/>

<%
            
Resource jsonResource = resourceResolver.getResource("/content/dam/adapttoaem/dialog/json/language.json/jcr:content/renditions/original/jcr:content");

if (jsonResource != null) {
java.io.InputStream stream = jsonResource.adaptTo(javax.jcr.Node.class).getProperty("jcr:data").getBinary().getStream();
org.json.simple.JSONArray jsonArray =  (org.json.simple.JSONArray) new JSONParser().parse(new java.io.InputStreamReader(stream));
java.util.Iterator<org.json.simple.JSONObject> jsonObjectIterator = jsonArray.iterator();
//ArrayList to hold data
java.util.List<Resource> resourceList = new java.util.ArrayList<Resource>();
String text = "", value = "";
while(jsonObjectIterator.hasNext()){
org.json.simple.JSONObject eachJsonObject = jsonObjectIterator.next();
value = (String)eachJsonObject.get("value");
text = (String)eachJsonObject.get("text") ;

//populate the map
ValueMap vm = new ValueMapDecorator(new java.util.HashMap<String, Object>());
vm.put("value",value);
vm.put("text",text);

resourceList.add(new ValueMapResource(resourceResolver, new ResourceMetadata(), "nt:unstructured", vm));
}
//Create a DataSource that is used to populate the drop-down control
request.setAttribute(DataSource.class.getName(), new SimpleDataSource(resourceList.iterator()));
}
%>

Highlighted (in yellow color)  path in above code is the JSON file which we dropped inside DAM.

Here I am using json-simple bundle to parse the InputStream in to JSON Array. Below is the maven dependency and link(can directly download and install in Felix Console).

<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple/1.1.1

Output:

Now lets create a page and drop our Demo component and open the dialog.


ta-da



final-dialog


Hurrah.... Here we go, all the values are populated inside the dropdown.

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

See how to Create a dynamic dropdown(Javascript) based on value of other dropdown


-AG