Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Dynamically populate selection dropdown in a dialog with a datasource using JSON

Avatar

Level 4

Hi ,

 

I want to create a "Dynamically populate selection dropdown in a dialog with a datasource using JSON" have any perfect reference please ping me how to do step wise function.

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor
6 Replies

Avatar

Correct answer by
Employee Advisor

Avatar

Community Advisor

@keshava219  You can create datasource node structure with sling:resourceType of datasource as sling:resourceType (String) = /bin/dropdownList ( i.e. Path of the servlet implementing the data source for dynamic value). It will give the desire result of a dynamic dropdown as you can see below the screenshot.

shaileshbassi_0-1657031259172.jpeg

 

    

shaileshbassi_1-1657031259273.jpeg

 

For DataSource Servlet (/bin/dropdownList) Implementation you can refer to below articles:

https://redquark.org/aem/day-17-granite-datasources/ 

http://www.sgaemsolutions.com/2019/01/dynamically-populate-drop-down-values.html 

Hope this helps

Thanks

Avatar

Level 4

hi @ShaileshBassi,

 

                 Thanks, this was helpfull But  I want to pass along with parameters as well to servlet from dialog which author mention

Example 

keshava219_0-1657196305176.png

 

             On dialog  i have numerical number i want to pass to servlet  this value  possible?

Avatar

Level 7

Hi @keshava219 

you should write dialog js so that wehn you change on dialog level at that time only dropdown value get changed (at the same time )---> Js will help you on that...

 

But if you want to pass the value in servlet , it can be done like below mentioned code snippet . but this would not be "on the fly" ..Suppose value is 5 .after dialog submission only your dropdown items count  would be 5   ..when you made it suppose 6 then submit dialog again and open it again then only 6 items you will be able to see ..In backedn it will work like that 

 

as you registered servlet with path ...just read the  dailog values of your field "Number of Articles to Show " in your servlet 

smething like this you can do 

 

then you can use this value and put the logic to just those many dropdown selection items .

 


String cmptOnPage = req.getPathInfo().toString();
String actualPath = cmptOnPage.substring(cmptOnPage.lastIndexOf("/content"), cmptOnPage.length());
ValueMap actualResourceValueMap = req.getResourceResolver().getResource(actualPath).adaptTo(ValueMap.class);
String dialogVlaues =actualResourceValueMap.get("text", String.class);

logger.info("dialogVlaues path "+dialogVlaues);


/** ------------------------------------------------------------