Copy link to clipboard
Copied
Hi,
I have a dropdown menu ("Dropdown1"), that I need to populate with options from the object I have defined below. How would I go about populating Dropdown1 with the names (Name1, Name2, & Name3) from the Class1 array?
var oClassList =
{
Class1:
[
{name: "Name1", company: "Company1", instructor: "Instructor1"},
{name: "Name2", company: "Company2", instructor: "Instructor1"},
{name: "Name3", company: "Company3", instructor: "Instructor1"}
],
Class2:
[
{name: "Name1", company: "Company1", instructor: "Instructor2"},
{name: "Name2", company: "Company2", instructor: "Instructor2"},
{name: "Name3", company: "Company3", instructor: "Instructor2"}
],
Class3:
[
{name: "Name1", company: "Company1", instructor: "Instructor3"},
{name: "Name2", company: "Company2", instructor: "Instructor3"},
{name: "Name3", company: "Company3", instructor: "Instructor3"}
]
};
I tried the following code using the setItems method, but I couldn't get it to work:
this.getField("Dropdown1").setItems(oClassList.Class1.name);
Copy link to clipboard
Copied
It must be an array. Read the documentation of setItems.
Copy link to clipboard
Copied
You would have to create a temp array variable that holds the names you want to assign to the dropdown. I would write a function that takes as arguments the class name and the class structure and then extracts the relevant names and returns them in an array variable that you can then use in the setItems() method. There is no direct way go go from the data structure you have to setting the dropdown values.
Copy link to clipboard
Copied
You can do it like this:
var names = [];
for (var i in oClassList.Class1) names.push(oClassList.Class1[i].name);
this.getField("Dropdown1").setItems(names);