Here's the code for a BindableComboBox. Save it in a file called
BindableComboBox.mxml (borrowed this code from Adobe, modified slightly):

 

<mx:ComboBox xmlns:mx="http://www.adobe.com/2006/mxml";
creationComplete="initComponent()">

  <mx:Script>

    <![CDATA[

      import mx.utils.ObjectUtil;

      import mx.controls.Alert;

 

      [Bindable] public var valueField:String = "";

      [Bindable] public var labelFields:Array = [];

 

      public function initComponent():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 {

        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) {

              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>

 

Use it in your components as follows:

 

 Get the data back from the database (or webservice, or wherever) into an
ArrayCollection that has the fields myID and myName for each item in the
combo box. Then, assign the AC to the dataProvider of the BindableComboBox
and use the component in your mxml code like this:

 

 <mycoms:BindableComboBox id="myComboBox" valueField="myID"
labelFields="[myName]"/>

 

I also have a search-as-you-type BindableComboBox that's a combination of
this component with an auto-complete component I found on the web (not
Adobe's)  if you're interested.

 

HTH,

~randy

 

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Tracy Spratt
Sent: Wednesday, October 08, 2008 10:05 AM
To: [email protected]
Subject: RE: [flexcoders] ComboBox in Component won't open to Saved Value

 

ComboBox.selectedItem will only work if you assign a *reference to an item
in the ComboBox dataProvider*.  This is rarely possible and is not in your
case.

 

What you must do is loop over the items in the CobmoBox.dataProvider and
compare the appropriate property's value to the value you want to match.
When a match is found, use the loop indes to set the ComboBox's
selectedIndex.  This is simple enough to do in a one-off situation.  

 

If you need this often, then you might want to use an extended ComboBox.
Making a generic one is a bit tricky, because the Combo Box dataProvider
items can have any properties at all.  I have an example on www.cflex.net
<http://www.cflex.net/>  (it allows you to set the data field you want to
match) and Ben forta has done one and there are others.

 

Tracy

 

  _____  

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Dan Pride
Sent: Wednesday, October 08, 2008 10:01 AM
To: [email protected]
Subject: [flexcoders] ComboBox in Component won't open to Saved Value

 

Hi
I have a ComboBox used as a Popup on a form component.
It saves fine using the following function

private function closeGenderPop(event:Event):void {
ComboBox(event.target).selectedItem.label}; 


I want to have it display the stored value the next time it is opened.
I tried this but no luck. 

private function openGenderPop(event:Event):void {
genderPop.selectedIndex = 1; 
ComboBox(event.target).selectedItem.label; }

Really appreciate the help

Dan Pride

 

Reply via email to