Saturday 7 October 2017

How to create dynamic drop down in Touch UI dialog in AEM 6.2.

There are many occasion where we need to populate drop down value dynamically. There is already example provide in AEM in list component at location /libs/foundation/components/list. In this example a folder "displayasdatasource" is create and under the folder a JSP "displayasdatasource" has logic to set dropdown value.

AEM uses Datasource to hold the dynamic value . we need to create a map which have dropdown value and add that map to datasource.

In this blog we will write the same logic in servelet and will invoke the servelet from dropdown node. Below are the steps:-

1) Create a component in Touch UI and created a field drop down field in dialog as shown in below pic.



2) Write the servlet to populate the datasource as below.
package com.aemnote.service.core.servlets;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceMetadata;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.sling.api.wrappers.ValueMapDecorator;

import com.adobe.granite.ui.components.ds.DataSource;
import com.adobe.granite.ui.components.ds.EmptyDataSource;
import com.adobe.granite.ui.components.ds.SimpleDataSource;
import com.adobe.granite.ui.components.ds.ValueMapResource;
@SlingServlet(paths = "/bin/availableThemes")
public class AvailableThemesServlet extends SlingSafeMethodsServlet {
private static final long serialVersionUID = 1668099305241096740L;
@Override
protected void doGet(SlingHttpServletRequest request,
SlingHttpServletResponse response) throws ServletException,
IOException {
List<Resource> themes = new ArrayList<Resource>();
// set fallback
ResourceResolver resolver = request.getResourceResolver();
request.setAttribute(DataSource.class.getName(),
EmptyDataSource.instance());
ValueMap vm = null;
for (int i = 0; i < 5; i++) {
// allocate memory to the Map instance
vm = new ValueMapDecorator(new HashMap<String, Object>());
// Specify the value and text values
String Value = "value" + i;
String Text = "text" + i;
// populate the map
vm.put("value", Value);
vm.put("text", Text);
themes.add(new ValueMapResource(resolver, new ResourceMetadata(),
"nt:unstructured", vm));
}
DataSource dataSource = new SimpleDataSource(themes.iterator());
request.setAttribute(DataSource.class.getName(), dataSource);
}
}
3) When your dialog load then it will call the above servlet and populate the dropdown note.

Note :- Please add "/" before bin in your servlet path otherwise it will not able to locate the servlet. Ex - /bin/availableThemes 


4.
Please let me know if you face any issue.



2 comments: