Expand my Community achievements bar.

SOLVED

Default value to select in Dropdown AEM 6.4

Avatar

Level 3

Hi All,

We have a requirement that in the Touch UI dropdowns we have to populate the value dynamically(some logic) using datasource. Now there is 3 drop down in a dilaog and 3 dropdown should have 3 different default value . The datasource is same for all three dropdown. Is there any way in AEM 6.4 to set the default value in  dialog. We are able to populate the values in dropdown using datasource but not able to set the default values.

sling:ResourceType: granite/ui/components/foundation/form/select

Also is AEM 6.4  provides typehead or Autocomplete drops down? we required this because the dropdown list is too large.

Appreciate for your response .

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@brijesht7477132 

In order to avoid additonal code for datasource servlet/ sling model.

We can use cq:template node to set initial default values for any kind of select datasource, textfield, radiogroup, checkbox etc. resource type.

Just add cq:teamplte node inside AEM component.

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0"
jcr:primaryType="nt:unstructured"
<jcrPropertyName>="<default value here>"/>

 

0 Replies

Avatar

Community Advisor

Hi,

How are you setting default value from datasource?

for me works fine to set default value for desire item(option)

vm.put("value", Value);

vm.put("text", Text);

vm.put("selected", true);

Request you to move this in AEM community.

Thanks

Arun

Avatar

Level 3

Hi Arun,

Thanks for your response. Actually we need something like emptyText which can handle at front end. I tried using emptyText but with datasource it is not working.

Also is it possible to pass any parameter form the datasource to java class ?

Avatar

Community Advisor

I tested emptyText  in AEM 6.3, it works with datasource if you use Coral3 components.

granite/ui/components/coral/foundation/form/select

You might be using Coral2 components granite/ui/components/foundation/form/select

Yes, It is possible to pass value in datasource, just create a property in datasource node and assign any value.

In servlet get the request path and from request path get datasource node resource and adaptTo Node and get property value and use it in servlet.

e.g. I created  dropdownSource property in datasource node and accessing using below code

  resourcePath = request.getRequestPathInfo().getResourcePath();

  resourcePath = resourcePath.replaceFirst("/.*(apps/)(.*$)", "/$1$2/");

  logger.info(resourcePath);

  Node itemNode = request.getResourceResolver().getResource(resourcePath).adaptTo(Node.class);

  if(itemNode.hasProperty("dropdownSource")) {

// do something

}

Thanks

Arun

Avatar

Level 3

Hi Arun,

Thanks for the response. As you suggested putting  vm.put("selected",true), The code started breaking with the below exception. It successfully set the selected to true but later it break and once i remove the selected to true it works fine

Can you kindly guide or share your code snippet. Also below sharing my lines of code.

Code

ValueMap labelValues = new ValueMapDecorator(new HashMap<String, Object>());

String labelKey = (String)labelhit.getResource().getValueMap().get(FollettConstants.LABELKEY_PROPERTY,null);

if(! StringUtils.isEmpty("author_by_prefix") && "author_by_prefix".equals(labelKey)) {

labelValues.put("selected", true);

}

labelValues.put("text", labelKey);

labelValues.put("value", labelKey+FollettConstants.HASH_SEPRATOR+labelhit.getPath());

labelList.add(new ValueMapResource(resourceResolver, new ResourceMetadata(),

     "nt:unstructured", labelValues));

    DataSource dataSource = new SimpleDataSource(labelList.iterator());

            request.setAttribute(DataSource.class.getName(), dataSource);

Exception

  23.07.2018 01:36:55.941 *ERROR* [0:0:0:0:0:0:0:1 [1532289997383] GET /mnt/override/apps/follett/components/content/labeldemo/pdpmappedlabels/_cq_dialog.html/content/follett/en/tcustore/pdppage/jcr:content/par/pdpmappedlabels HTTP/1.1] org.apache.sling.engine.impl.SlingRequestProcessorImpl service: Uncaught SlingException

org.apache.sling.api.request.RecursionTooDeepException: /mnt/override/apps/follett/components/content/labeldemo/pdpmappedlabels/cq:dialog/content/items/mappedlabel/items/column/items/addtobag

at org.apache.sling.engine.impl.request.RequestData.setContent(RequestData.java:571) [org.apache.sling.engine:2.6.12]

at org.apache.sling.engine.impl.SlingRequestProcessorImpl.dispatchRequest(SlingRequestProcessorImpl.java:311) [org.apache.sling.engine:2.6.12]

at org.apache.sling.engine.impl.request.SlingRequestDispatcher.dispatch(SlingRequestDispatcher.java:211) [org.apache.sling.engine:2.6.12]

at org.apache.sling.engine.impl.request.SlingRequestDispatcher.include(SlingRequestDispatcher.java:104) [org.apache.sling.engine:2.6.12]

Avatar

Correct answer by
Community Advisor

@brijesht7477132 

In order to avoid additonal code for datasource servlet/ sling model.

We can use cq:template node to set initial default values for any kind of select datasource, textfield, radiogroup, checkbox etc. resource type.

Just add cq:teamplte node inside AEM component.

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0"
jcr:primaryType="nt:unstructured"
<jcrPropertyName>="<default value here>"/>

 

Avatar

Level 2

@Suraj_Kamdi 

Can you send screenshot of this node.

I tried created node but it is not resolving the purpose

page footer