Flex60460
Flex60460

Reputation: 993

<s:DropDownList selectedItem

I'd to use my database field value to select a value on my <s:DropDownList

I try to do

<s:DropDownList  dataProvider="{DP_PAT_CIVIL}" selectedItem="@{objectUser.usrQualParent}"/>

But no selection appear, nothing appear on prompt.

objectUser.usrQualParent represent a value with is present in the dataprovider DP_PAT_CIVIL.

For exemple, dataprovider is:

[Bindable]
        private var DP_PAT_CIVIL:ArrayCollection = new ArrayCollection (
            [{label:"Monsieur" , data:"0"},
            {label:"Madame", data:"1"},
            {label:"Mademoiselle", data:"2"},
            {label:"Le Jeune", data:"3"}]

            );

And objectUser.usrQualParent value is "Monsieur".

Thanks for helping

Upvotes: 3

Views: 6335

Answers (1)

Constantiner
Constantiner

Reputation: 14221

Selected item should point to exact item from data provider. For simple types of which data provider can consist (like String, Boolean or int) it can be equal that value. For complex objects it should be exactly the same value (pointer to the same memory's unit).

So, in your case, "Monsieur" isn't an item of data provider which consists of Objects but not Strings. But the following isn't the way too:

<s:DropDownList  dataProvider="{DP_PAT_CIVIL}" selectedItem="{{label:"Monsieur" , data:"0"}}"/>

because of it is newly created but not the same object.

I recommend you create some function for searching exactly the same value from the existing data provider using your string as a key:

private function getSelectedItem(dp:ArrayCollection, key:String):Object
{
    if (dp && dp.length > 0)
    {
        for each (var item:Object in dp)
        {
            if (item.label == key)
                return item;
        }
    }
    return null;
}

Now your list:

<s:DropDownList  dataProvider="{DP_PAT_CIVIL}" 
    selectedItem="{getSelectedItem(DP_PAT_CIVIL, objectUser.usrQualParent)}" 
    change="objectUser.usrQualParent = event.currentTarget.selectedItem.label"/>

Upvotes: 6

Related Questions