--- In flexcoders@yahoogroups.com, "flexjeremy" <[EMAIL PROTECTED]> wrote:
>One last thing if I
> were getting a value out of the database is there a way to say that
> item is selected? 

I've written a class that allows you to data-bind a combo similar to
how you bind a text field..  You set the valueField propertie on an
instance of this component to the field in the dataProvider that
you're using as the "data" or key field, then you bind the
bindableValue property to your dataModel in both directions.  

So, saw you have a form that you're using to track customer's
addresses, and this form is backed by a  customerAddress object with
properties like "city", "state", and "zipCode".  You also have an
ArrayCollection with state names and abbreviations - states.stateName
and states.stateCode..  You would set the combo's dataProvider to
states, and it's labelField to stateName.  For the custom properties,
you set the valueField property to stateCode, and then two-way bind
the bindableValue property to customerAddress.state.

Any time the user selects a new state from the combo, the component
will update the bindableValue property with the corresponding
valueField value - data binding changes it in your model.  When the
model changes programmatically, eg from the result of a remoting call,
data binding will update bindableValue from the model..  The
propertyChange event is issued and caught by the component, and it
calls code exactly like what's been posted to locate the item with the
corresponding key.

Code:


<?xml version="1.0" encoding="utf-8"?>
<mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml";
change="onChange(event)" creationComplete="setup()">
        <mx:Script>
                <![CDATA[
                        import mx.controls.Alert;
                        
                        public var valueField:String;
                        [Bindable] public var bindableValue:Object;
                        
                        private function setup():void
                        {
                                this.addEventListener("propertyChange", 
selectByValue); 
                        }
                        
                        public function onChange(e:Event):void
                        {
                                if (bindableValue != 
this.selectedItem[valueField])
                                {
                                        bindableValue = 
this.selectedItem[valueField];
                                }
                        }
                        
                        public function selectByValue(e:Event):void
                        {
                                for (var i:Number = 0;  i < 
this.dataProvider.length;  i = i + 1)
                                {
                                        if 
(this.dataProvider[i][this.valueField] == bindableValue)
                                        {
                                                this.selectedIndex = i;
                                                return;
                                        }
                                }
                                //If we're still here we got passed a value 
that's not in the combobox
                                this.selectedIndex = -1;
                        }
                ]]>
        </mx:Script>
</mx:ComboBox>

Reply via email to