There’s other (possibly more elegant) solutions out there on the web, but I came up with this today when faced with trying to set the initial “selectedIndex” of a ComboBox whose contents are populated dynamically. Unlike HTML, it’s not as simple as just marking one of the “options” as selected, but fortunately, it’s not a lot harder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <mx:Script> <![CDATA[ private function locateLabel( source : ArrayCollection, label : String ) { for( var i=0; i<source.length; i++ ) { if(source[i].label == label ) return i; } return 0; } ]]> </mx:Script> <mx:ComboBox dataProvider="{model.stateList}" selectedIndex="{locateLabel(model.stateList,model.address.state)}" /> |
This method triggers binding whenever model.stateList or model.address.state changes, and updates the combo-box position accordingly. To me this is a bit cleaner than sub-classing the ComboBox, which was the primary other method that I saw recommended, but I welcome other suggestions, if there’s an even easier method I’ve missed.