Tahir Alvi
Tahir Alvi

Reputation: 994

How i set index of an item in combo box in flex?

I want to set the items order that are in an array Collection and bind with a combo-box.

here is my code

[Bindable]private var langList:ArrayCollection = new ArrayCollection([{label:"Englis"},{label:"Urdu"},{label:"Arabic"},{label:"Spanish"}]);

protected function cbm_creationCompleteHandler(event:FlexEvent):void
            {
                for(var i:int =0; i< langList.length; i++)
                {

                    if(langList[i].label == 'Urdu')
                    {
                        cbm.setChildIndex(cbm.getChildAt(i), 0);
                        break;
                    }
                }


            }
<s:ComboBox  id="cbm" x="258" y="113" dataProvider="{langList}" creationComplete="cbm_creationCompleteHandler(event)"/>

when i try to run it the following exception throw by the compiler

RangeError: Error #2006: The supplied index is out of bounds.

Upvotes: 0

Views: 591

Answers (1)

Kodiak
Kodiak

Reputation: 5978

Be careful, ComboBox::getChildAt won't return the list item, you shouldn't access the children of a Flex a component, this is a skin matter.

If you want to change the elements' order you must do it in the dataProvider, this is a data matter.

for(var i:int =0; i< langList.length; i++)
{
    var item:Object = langList[i];
    if(item.label == 'Urdu')
    {
        langList.removeItemAt(i);
        langList.addItemAt(item, 0);
        break;
    }
}

Since your langList is Bindable, the combobox will be automatically updated.

Upvotes: 2

Related Questions