0 Replies Latest reply on Jul 15, 2011 3:28 PM by cilantro11

    value bound dropdownlist???

    cilantro11

      In Flex 3, I often used a custom combobox that was set up to automatically select a choice that corresponded with a choice from a database record. For instance, if I opened a record for "Fred" and Fred had selected "blue" as his favorite color, the combobox would be set to "blue". The combobox was always some variation of the code found here: http://flashflex.com/flex-examples-binding-a-value-to-a-combobox/

       

      Now I'm trying to use Flex 4 and Flash Builder prompts me to use <s:DropDownList> in the place of <mx:ComboBox>. The problem is, I can't seem to write a customized version of the DropDownList that does what my custom ComboBox did. I've done all kinds of searches and made some attempts, but the closest I get is a DropDownList that will go to the choice from the database record *after* I click on it. So I'm using my trusty customized ComboBox solution until I can spend time to figure this out. Can anyone help?

       

      (Apologies for any typos... I can't transfer my code directly here and am typing it out...)

       

      ValueBoundComboBox.as (this works)

       

      package aaa.bbb.ccc.components {

       

           import flash.events.Event;

           import mx.controls.ComboBox;

       

           public class ValueBoundComboBox extends ComboBox {


           private var dataProviderChanged:Boolean=false;
           private var selectedValueChanged:Boolean=false;
           public var dataProperty:String="data";
           private var _selectedValue:*;

           public function ValueBoundComboBox() {
                super();
           }

           override public function set dataProvider(value:Object):void {
                 dataProviderChanged=true;
                 super.dataProvider=value;
           }

           public function get selectedValue():* {
                return _selectedValue;
           }

                
           public function set selectedValue(val:*):void {
                _selectedValue=val;
                selectedValueChanged=true;
                invalidateProperties();
           }

           override
      protected function commitProperties():void {
               super.commitProperties();
            
               if ((selectedValueChanged == true && dataProvider != null) ||
                    (dataProviderChanged == true && selectedValue != undefined)){
                        
                          dataProviderChanged=false;
                          selectedValueChanged=false;

                          var idx:int=-1;

                          for (var i:int=0; i < dataProvider.length; i++) {
                              if (this.dataProvider[i] != null &&
                                  this.dataProvider[i]as String == selectedValue)
                              {
                                  idx=i;
                                  break;
                              }
                          }

                          this.selectedIndex=idx;
                      }
                }

                override protected function collectionChangeHandler(event:Event):void
                  {
                      super.collectionChangeHandler(event);

                      dataProviderChanged=true;
                      invalidateProperties();
                  }
      }

      }

       

      ****************************************************************************************** *********************

       

      ValueBoundDropDownList.as (this does not work)

       

      package aaa.bbb.ccc.components {

       

           import flash.events.Event;

           import mx.collections.IList;

           import spark.components.DropDownList;

       

           public class ValueBoundDropDownList extends DropDownList {


           private var dataProviderChanged:Boolean=false;
           private var selectedValueChanged:Boolean=false;
           public var dataProperty:String="data";
           private var _selectedValue:*;

           public function ValueBoundComboBox() {
                super();
           }

           override public function set dataProvider(value:IList):void {
                 dataProviderChanged=true;
                 super.dataProvider=value;
           }

           public function get selectedValue():* {
                return _selectedValue;
           }

                
           public function set selectedValue(val:*):void {
                _selectedValue=val;
                selectedValueChanged=true;
                invalidateProperties();
           }

           override
      protected function commitProperties():void {
               super.commitProperties();
            
               if ((selectedValueChanged == true && dataProvider != null) ||
                    (dataProviderChanged == true && selectedValue != undefined)){
                        
                          dataProviderChanged=false;
                          selectedValueChanged=false;

                          var idx:int=-1;

                          for (var i:int=0; i < dataProvider.length; i++) {
                              if (this.dataProvider[i] != null &&
                                  this.dataProvider[i]as String == selectedValue)
                              {
                                  idx=i;
                                  break;
                              }
                          }

                          this.selectedIndex=idx;
                      }
                }

               
      }

      }