Highlighted

How to add values to a dropdown upon initialize

johnh86027037

03-05-2018

I am creating a dropdown list - where users can choose the proper year of business.

This dropdown needs to be customized to the user:

  • Values begin at the year the business began to generate hazardous waste
  • I need to add values for each year - up to the current year.

I want the current year to be calculated instead of static.

How do I add values to a dropdown list?

In LiveCycle - I would do something like this:

    

for (var i=dateBaseYear.rawValue; i<=dateCurrentYear.rawValue; i++)

{

//console.println(i);

this.addItem("" + i); //needed to add a string in front of 'i'.  not sure why

}

Is there a similar solution for AEM Forms?

Replies

Highlighted
Highlighted

johnh86027037

03-05-2018

I have saved the Guide and refer to it constantly.

Is there an example that I can reference?  I cannot seem to find out to add each value - via the rule editor.

Thanks

Highlighted

DarrenBiz

26-08-2018

The DropDownList can be programmatically populated in the initialise event using an array of key=value pairs [1] on the items property [2]. So for your particular example, to populate the list starting at the current year and running for 5 years would look something like:

(on the initialise event)

var currentYear = (new Date()).getFullYear();

this.items = [];

for (var i = 0 ; i < 5 ; i++) {

  this.items.push(i + "=" + currentYear-- + " Item Name");

}

The above code will add 5 entries to the drop down list

0=2018 Item Name

1=2017 Item Name

...

4=2014 Item Name

When the item is selected, the key will be stored in the field property. If you want the value stored instead, then just leave out the key= part to the item string when you are adding it to the items array.

[1] Adobe Experience Manager Help | Adaptive Form Expressions

[2] Adaptive Forms Class: GuideDropDownList