开发者

How to set selectedIndex for Flex DownDownList

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开发者_C百科 didn't work.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜