tags:

views:

11

answers:

1

I have an Array Collection as follows

var myArr:ArrayCollection = new ArrayCollection([
                {label: "label1", data: "data1"}, 
                {label: "label2", data: "data2"}]); 

And a dropdownlist

<s:DropDownList id="myDDL"
                dataProvider="{myArr}" />

How to set the selected item for myDDL based on an item id(myID)? I tried something like:

selectedIndex = {myArr.getItemIndex(myID)}

But it didn't work.

+2  A: 

Did you set the selectedIndex in MXML? Or ActionScript? Since you use binding syntax, it makes me think that you want to use MXML. You can't bind to the results of a function; only to a property. Internally, binding uses "propertyChange" events so that Flex knows when a value changes. The event is dispatched in the setter function (for most properties implemented in the Flex Framework).

There is no equivalent parallel for functions. You should be able to do an assignment in AS3 w/o any problems, though:

selectedIndex = myArr.getItemIndex(myID)

You need to make sure that 'myID' is an element of your dataProvider, though. Something like this should, in theory, work:

 myID = {label: "label1", data: "data1"}

However, it may be possible that the 'getItemIndex" checks for the memory location, not an actual value comparison. If so, then you'll need to do something like this:

 myID = myArr[0]

For the code to work.

www.Flextras.com
Well explained. Thanks.
Tam