R – ComboBox selectedItem in Flex3

actionscript-3airapache-flexflex3

i am working in Air application in Flex3 , i need know how to set "selectedItem" Property when we have 2 values like(data and label) label property to combobox selection, data value for our input.

Like shown below.

In (selectedItem="{stylename}") stylename will have "data" value but i need to set that "lable" property as selected value in combobox.

Like if stylename is "checked" then the ComboBox selected item need to be "Checked".

How to implement this in flex ….

Thanks in Advance

Best Answer

ComboBox.selectedItem is looking for an Object. You are passing it a String literal. Where is "stylename" being set? If this is coming from an external source, you can retrieve the item to be selected in a setter function:

ActionScript 3:

[Bindable]
public var comboBoxData:ArrayCollection;

[Bindable]
private var comboBoxSelectedItem:Object = {};

private var _styleName;

private function get styleName():String
{
    return _styleName;
}

private function set styleName(value:String):void
{
    _styleName = value;

    comboBoxSelectedItem = getItemFromCollection("styleName", value);
}

private function getItemFromCollection(property:String, value:String):Object
{
    // Create a copy of the Collection used as the dataProvider for the ComboBox
    var filteredCollection:ArrayCollection = 
        new ArrayCollection(comboBoxData.toArray());

    // Set a filterFunction to filter only those Objects with the specified name/value pair
    filteredCollection.filterFunction = 
        function(item:Object):Boolean
        {
            return item[property] == value;
        }

    // Refresh the collection to apply the filterFunction
    filteredCollection.refresh();

    // Return an empty Object if no Object was found with the given name/value pair
    if (filteredCollection.length == 0)
        return {};

    // Return the first/only Object in the filtered Collection
    return filteredCollection.getItemAt(0);
}

MXML:

<mx:ComboBox dataProvider="{comboBoxData}" selectedItem="{comboBoxSelectedItem}" />