Tong Wang
Tong Wang

Reputation: 1612

Flex Combobox: how to get the value of the selected item?

I am using a combobox for the us states, link. The label is set to the full name of the state, while the value attribute holds the abbreviation. What I want to do is to get the selected item's value. So I tried combo.selectedItem.value and combo.selectedItem.@value, but neither of them worked. Can someone shed a light on this please?

Upvotes: 1

Views: 15738

Answers (2)

Emanuil Rusev
Emanuil Rusev

Reputation: 35265

Here's a simple example that might be helpful.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
 <mx:ComboBox id="comboBox" dataProvider="{[{label:'California', value:'CA'}, {label:'New York', value:'NY'}]}" />
 <mx:Label text="{comboBox.selectedItem.value}" />
</mx:Application>


Here's another example. In this one we use XML as dataProvider.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
    <mx:XML id="xml" xmlns="">
        <states>
            <state label="Alabama" value="AL" country="US" />
            <state label="Alaska" value="AK" country="US" />
            <state label="Arkansas" value="AR" country="US" />
        </states>
    </mx:XML>
    <mx:ComboBox id="comboBox" dataProvider="{xml.state}" labelField="@label" />
    <mx:Label text="{comboBox.selectedItem.@value}" />
</mx:Application>

Upvotes: 9

Diego Dias
Diego Dias

Reputation: 22646

You can populate an array with the values you want to get and retrieve the index of the selected item on the combo box (which should be the same as in the array).

Or in your component ... just look for the index (selected item) child on statesUS

Upvotes: 0

Related Questions