Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events

Display default text on dropdown with default value

Avatar

Community Advisor

Hi,

I've created a drop down selection field in dialog and provided it some default value with property key defaultValue and assigned some value it which is getting displayed when I open the dialog box. What I want is to display some default text with the default value in interger format. 

Requirement is, this is optional field in my dialog and when I create json for this page(created our own json generator), it displays in  json like:

{ linktarget : "--Please select text--" }

which I don't want. And, can't put check if this value(defaultValue) i.e if this comes, don't add in json object bcz if due to some reason name gets changed then again it'd be a java change which I can't afford. So, rather than that, if could provide defaultValue some interger(say, 0) which we'll remain same every time if no property get choosen, no matter what text we display.

Thanks,

Himanshu

3 Replies

Avatar

Administrator

Hi 

Please check this article. I hope this would help you.

Link:- https://myprogressivelearning.wordpress.com/2015/01/21/setting-default-value-to-the-drop-down-of-dia...

//Setting Default value to the drop down of Dialog in Adobe AEM

To achieve this, We should have a listener while loading of the dialog which populates the values dynamically and that should set the defaultValue also. In this case,optionsProvider property is not required as this listener itself can set the options while loading itself.

The reason for setting the default value using listener is, defaultValue property does not accept the script or a JSON. It accepts only the String value. So the listener namely loadcontent (called when the dialog loads) should be used to set the default.

Below code will set the first element of JSON as the defaultValue.

     
1
2
3
4
5
6
7
8
9
10
function(dialog){
var selection = dialog.getField("./image"); // Getting the drop down by it's name
 
 
 /*
*/  
   selection.setOptions(images); // Setting the options to the drop down
    selection.setValue(images[0].value); // setting the defaultValue of drop down
}

name of the listener has to be listeners. Since we are doing this operation while loading of the dialog content the listeners node should be the direct child node of the dialog.

Properties of drop down:
dropdown

Properties of listeners:

listeners

This will show the first value of the options as the default value while opening the dialog itself.

Note:
1. listener is the node of nt:unstructured type which typically have the events as properties. Example for events are,
loadcontent – while loading the content the script of that property will be called.
beforesubmit – before pressing the OK button
for drop down we have few listener event like selectionchanged, etc.,

2. defaultValue should be the value of the option provided. If we give text, it will not work.

3. defaultValue and allowBlank will not go together sometimes. When allowBlank is false, what ever value be the defaultValue, it will take the first value as
defaultValue. So when ever defaultValue is provided, we don’t have to provide allowBlank property as false. It is meaningless!

I hope this would help you.

 

Thanks and Regards

Kautuk sahni



Kautuk Sahni

Avatar

Community Advisor

Thanks for the help! But, I didn't get much of it. 

What I need is if I didn't select any of value from drop-down, then, value I get from back-end should come as some integer value. So, that time, I can put a check condition, if value is coming some integer(say zero, if default selected), then don't add this property in json.

HImanshu,

Would you mind explaining your use case in details. In what case, do you generate json and does it driven from drop down values?.

Jitendra