• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Setting and getting a drop down list selection

Mentor ,
Aug 03, 2012 Aug 03, 2012

Copy link to clipboard

Copied

Hi,

This is a general ES question, but since I'm working with FM, I'll try here first.

I am able to create dropdown lists with ES and set/get the current selection, but I'm not clear on why it is working the way it does. It seems that when I set the selection, it uses an index of the current items, but when I get the selection, it gets the actual value.

Consider this sample, with its comments:

//create the dropdown, for the dlg dialog

dlg.dropDown = dlg.add("dropdownlist", undefined, [10, 20, 30]);

//this sets the current value in the box to 30

dlg.dropDown.selection = 2;

//however, this reports 30, not 2

alert(dlg.dropDown.selection);

Does anybody know about this? Is it true that it is an index when set, and a value when gotten? I couldn't find anything in the documentation about it.

Thanks,

Russ

TOPICS
Scripting

Views

6.8K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Mentor , Aug 03, 2012 Aug 03, 2012

Nevermind, I got it. The 'selection' property is actually a ListItem object, not just a primitive data type as I had thought. So, the default property of the ListItem is chosen differently in different scenarios. To clarify:

dlg.dropDown.selection.index

...is the index

dlg.dropDown.selection.text

...is the text value

So I should be using the full property to be sure.

Russ

Votes

Translate

Translate
Mentor ,
Aug 03, 2012 Aug 03, 2012

Copy link to clipboard

Copied

Nevermind, I got it. The 'selection' property is actually a ListItem object, not just a primitive data type as I had thought. So, the default property of the ListItem is chosen differently in different scenarios. To clarify:

dlg.dropDown.selection.index

...is the index

dlg.dropDown.selection.text

...is the text value

So I should be using the full property to be sure.

Russ

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 11, 2012 Sep 11, 2012

Copy link to clipboard

Copied

I'm having similar issues and this thread doesn't quite provide the answer.

In your original example:

//create the dropdown, for the dlg dialog

dlg.dropDown = dlg.add("dropdownlist", undefined, [10, 20, 30]);

//this sets the current value in the box to 30

dlg.dropDown.selection = 2;

//however, this reports 30, not 2

alert(dlg.dropDown.selection);

...

Say that you want to set the dropDown.selection, not with an index  number, but with the text.

Setting. dropDown.selection.text = "30" doesn't actually change the dropDown.selection. What it does is change the CURRENT dropDown.selection to say "30". (So, if you had "20" selected, it'd change it to say "30", but then your list would include "10, 30, 30"

Numerous pieces of my script need to select items in lists based on the string-name of the listItem. (The positions of the items change sometimes, so using the index doesn't work)

If my dropDown menu includes "a", "b", and "c", how do I get something like this to work:

>>>dropDown.selection = "a"?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Sep 11, 2012 Sep 11, 2012

Copy link to clipboard

Copied

Raemon_777,

I don't know a direct way to do this, but I can figure out a roundabout method. Here's what I know, which isn't all that much...

A dropdownlist object has an "items" property, which is an array of listitem objects that represent the selections. For example, in your list that contains "a", "b", and "c", you'll have three listitems in the array. Furthermore, each listitem has two important properties:

"text" which is the text value that displays

"index" which is the index in the "items" array, starting at 0 for the first one.

So, in the a,b,c example, you have these properties:

dropDown.items[0].text     (equals "a")

dropDown.items[0].index   (equals 0)

dropDown.items[1].text   (equals "b")

dropDown.items[1].index   (equals 1)

dropDown.items[2].text   (equals "c")

dropDown.items[2].index   (equals 2)

And, the "selection" property of the dropdown object is just a reference to one of these listitems object. So, if you want to make the list show "c", you could do:

dropDown.selection = dropDown.items[2]

...but, you say you don't know the index, just the text value. So, I worked up this little function which seems to do the trick. Send it the dropdownlist object and the text value you want displayed. It will find the correct listitem object and set it as the "selection" property, rather than your previous method that was simply resetting the text value of the current selection listitem.

Like I said, there might be a better way to do this. In a hurry, this is the best I could conjure up.

Russ

function SetDropDown(dropdown, value)

{

    for(i = 0; i < dropdown.items.length; i++)

    {

       if(dropdown.items.text == value)

       {

           dropdown.selection = dropdown.items;

           i = dropdown.items.length;

       }

    }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 12, 2012 Sep 12, 2012

Copy link to clipboard

Copied

Another solution will depend on how you populate the dropdownlist controls in the first place. If you build an array ahead of time, you can do this:

// Make an array for the drop down list.

var a = ["a", "b", "c"];

// Add a "property" to the array for each member.

for (var i = 0; i < a.length; i += 1) {

  a["_"+a] = i;

}

// Make a dialog box and dropdown list, using the array.

var dlg = new Window("dialog");

var dropDown = dlg.add("dropdownlist", undefined, a);

// Set the selection to "b".

dropDown.selection = a["_b"];

dlg.show();

This assumes that all of the members of your array are unique. Please let me know if you have any questions or comments.

Rick

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 02, 2022 Sep 02, 2022

Copy link to clipboard

Copied

LATEST

I know this is old!

 if(dropdown.items.text == value)

should be

 if(dropdown.items[i].text == value)

 

I had been using array.indexOf which works modern AE, but for some reason it is undefined if the script runs immediately when AE first starts up. If you run the script again, it is fine. My fix is to use the above method.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines