The selectedItem property is readonly, so you have to extend the component.

When Adobe deployed the ColdFusion wizard, apps, they built a version
of exactly what you are trying to do, but the implementation wasn't
too reliable as I recall.  I would suggest that as a starting point,
but it looks like you found it as well, as the code looks identical.

I as I recall, the biggest issue we encountered is the race
condition--if the combobox dataprovider is empty, you will have
nothing to loop over.

Maybe storing the selectedItem in your setter as an private variable
in the extended class, then then adding a listener to the data
provider would help; that way, if selectedItem is set before the data
is there, you can catch the new data coming in, and then set the
selectedItem after the cb contents have been updated.

HTH,
Scott

On Mon, May 4, 2009 at 10:36 AM, Chang, Shyue-jing
<[email protected]> wrote:
> Hi,
>
> Does anyone know how to bind the selectedItem of a combobox? Currently I am 
> having problems trying to bind the selectedItem of a bindable combox in a 
> master/detail app.  Here is my code for the combobox:
>
> <mx:FormItem id="relationshipFormItem" label="Relationship to XX" 
> fontWeight="bold">
>        <view:BindableComboBox id="relationshipCB" width="123"
>                dataProvider="{model.relationships}"
>                valueField="relationshipCode"
>                labelField="relationshipDesc" enabled="true"
>                selectedItem="model.selectedGuest.relationship}" />
> </mx:FormItem>
>
> I would like to have the selectedItem bind to the relationship of the 
> selectedGuest when a guest is selected form the master list. When a guest is 
> selected from the master list, the selectedGuest in the model is set to the 
> guest selected and the state in the model is set to the detail state.  Both 
> relationships and guest are bindable value objects.
>
> 1. the "relationships" value object has two properties relationshipCode and 
> relationshipDesc
> 2. the guest value object that has a relationship property (which is a string 
> that corresponds to the relationshipCode of the relationships VO) plus other 
> properties.
>
> What happened was that the selectedItem sometimes bind correctly to the 
> relationship of the selected guest, but sometimes did not bind and the first 
> value in the drop down box (null) was displayed.  I even had two exact 
> comboboxes (with different ids) and sometimes one did bind and the other not.
>
> Can someone shed some lights on this?  Any help will be deeply appreciated.
>
> Susan
>
> This is the code of the BindableCombox I used (provided by the code generator 
> for coldfusion/flex)
>
> BindableCombobox (provided by code generator for coldfusion/flex):
>
> <?xml version="1.0" encoding="utf-8"?>
> <!--
> ////////////////////////////////////////////////////////////////////////////////
> //
> //  Copyright (C) 2003-2006 Adobe Macromedia Software LLC and its licensors.
> //  All Rights Reserved. The following is Source Code and is subject to all
> //  restrictions on such code as contained in the End User License Agreement
> //  accompanying this product. If you have received this file from a source
> //  other than Adobe, then your use, modification, or distribution of this 
> file
> //  requires the prior written permission of Adobe.
> //
> // �...@author Dean Harmon
> // �...@author Mike Nimer
> ////////////////////////////////////////////////////////////////////////////////
> -->
> <mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml"; xmlns="*" 
> creationComplete="componentInit()">
>    <mx:Script>
>        <![CDATA[
>            import mx.utils.ObjectUtil;
>            import mx.controls.Alert;
>
>            [Bindable]
>                public var valueField:String = "";
>
>                [Bindable]
>                public var labelFields:Array = [];
>
>                        public function componentInit():void
>                        {
>                                this.labelFunction = renderLabelFunction;
>                        }
>
>                        public function renderLabelFunction(item:Object):String
>                        {
>                                var result:String = "";
>                                if (labelFields.length == 0)
>                                {
>                                        if (labelField != null)
>                                        {
>                                                return item[labelField];
>                                        } else {
>                                                return item.toString();
>                                        }
>                                } else {
>
>                                        for(var i:int=0; i < 
> labelFields.length; i++)
>                                        {
>                                                if (i > 0)
>                                                {
>                                                        result += " ";
>                                                }
>
>                                                result += item[labelFields[i]];
>                                        }
>                                }
>                                return result;
>                        }
>
>                        override public function set 
> selectedItem(val:Object):void
>                        {
>                                //Alert.show(valueField +":" 
> +ObjectUtil.toString(val));
>                                if( this.valueField != null )
>                                {
>                                        for(var i:int=0; i < 
> this.dataProvider.source.length; i++)
>                            {
>                                var item:Object = this.dataProvider.source[i];
>
>                                if( item[valueField] == val )
>                                {
>                                    // if it matches, make it selected.
>                                    this.selectedIndex = i;
>                                    break;
>                                }
>                            }
>                           }
>                           else
>                           {
>                                        super.selectedItem(val);
>                           }
>                        }
>
>                        public function get selectedItemValue():Object
>                        {
>                                if( this.valueField != null && selectedItem != 
> null)
>                                {
>                                        return selectedItem[valueField];
>                                } else {
>                                        return text;
>                                }
>                        }
>
>        ]]>
>    </mx:Script>
> </mx:ComboBox>
>
>
>
>
>
> -------------------------------------------------------------
> To unsubscribe from this list, simply email the list with unsubscribe in the 
> subject line
>
> For more info, see http://www.affug.com
> Archive @ http://www.mail-archive.com/discussion%40affug.com/
> List hosted by http://www.fusionlink.com
> -------------------------------------------------------------
>
>
>



-- 
Scott Talsma
CTO, echoEleven

Reply via email to