4 Replies Latest reply on Dec 7, 2009 4:31 AM by ejchen77

    Problem about changing dataprovider of Combobx

    ejchen77

      I want to change items in combobox dynamically, but there is a strange problem....

      The contents of combobox will not update imediatedly, I have to click one item to make it show new contents.

       

      For example, as belowing source code:

      There are 2 button, "Button1" and "Button2", and one combobox.

      I wish to do this:

      When clicking "Button1", the data in combobox will be "1", "2", "3", "4",

      When clicking "Button2", the data in combobox will be "One", "Two", "Three", "Four".

      However, in the runtime, when I click "Button2", the 1st item of combobox will be "One",

      but whe I drop down the combobox, it is still "1", "2", "3", "4". (as attached picture)

      I have to click one item, then the comobox will show  "One", "Two", "Three", "Four".

       

      How can I solve the problem? thanks!

       

      ------------------------------------------------------------------------------------------ --------------------------------------

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768">

       

          <fx:Script>
              <![CDATA[
                  [Bindable]
                  public var myArray1:Array = ["1", "2", "3", "4"];
                 
                  [Bindable]
                  public var myArray2:Array = ["One", "Two", "Three", "Four"];
                 
                  protected function button1_clickHandler(event:MouseEvent):void {
                      combo1.dataProvider = myArray1;
                  }
                  protected function button2_clickHandler(event:MouseEvent):void {
                      combo1.dataProvider = myArray2;
                  }

       

              ]]>
          </fx:Script>

       

          <fx:Declarations>
              <!-- Place non-visual elements (e.g., services, value objects) here -->
          </fx:Declarations>
          <s:Button x="70" y="33" label="Button1" click="button1_clickHandler(event)"/>
          <s:Button x="159" y="33" label="Button2" click="button2_clickHandler(event)"/>
          <mx:ComboBox id="combo1" x="69" y="73" dataProvider="{myArray1}"></mx:ComboBox>
             
      </s:Application>

        • 1. Re: Problem about changing dataprovider of Combobx
          chris.huston.t10 Level 3

          Arrays do not listen for change events. Use an ArrayCollection for binding when you need to respond to change events. The code below converted your array to an ArrayCollection and added the labelField parameter to the ComboBox.

           

          Chris

           

          <?xml version="1.0" encoding="utf-8"?>
          <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                         xmlns:s="library://ns.adobe.com/flex/spark"
                         xmlns:mx="library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768">
             
             
             
              <fx:Script>
                  <![CDATA[
                      import mx.collections.ArrayCollection;
                      [Bindable]
                      public var myArray1:ArrayCollection = new ArrayCollection([{label:"1"}, {label:"2"}, {label:"3"}, {label:"4"}]);
                     
                      [Bindable]
                      public var myArray2:ArrayCollection = new ArrayCollection([{label:"One"}, {label:"Two"}, {label:"Three"}, {label:"Four"}]);
                     
                      protected function button1_clickHandler(event:MouseEvent):void {
                          combo1.dataProvider = myArray1;
                      }
                      protected function button2_clickHandler(event:MouseEvent):void {
                          combo1.dataProvider = myArray2;
                      }
                     
                     
                     
                  ]]>
              </fx:Script>
             
             
             
              <fx:Declarations>
                  <!-- Place non-visual elements (e.g., services, value objects) here -->
              </fx:Declarations>
              <s:Button x="70" y="33" label="Button1" click="button1_clickHandler(event)"/>
              <s:Button x="159" y="33" label="Button2" click="button2_clickHandler(event)"/>
              <mx:ComboBox id="combo1" x="69" y="73" dataProvider="{myArray1}" labelField="label"></mx:ComboBox>
             
          </s:Application>

          • 2. Re: Problem about changing dataprovider of Combobx
            ejchen77 Level 1

            Hi Chris,

             

            Thanks, it is better.

            However, it doesn't solve the problem.

             

            Here is the testing scenario by using your code:

            1. load the page,

            2. click Button2

            The combox works well, switch to "One", "Two" ... and drop down list is also ok

            3. click Button1

            The 1st item of combox changed to '1', but the drop down list is still "One", "Two" ...

             

             

            Thanks for any further advice.

            Leon

            • 3. Re: Problem about changing dataprovider of Combobx
              chris.huston.t10 Level 3

              Yes, I see what you mean. Can you use a DropDownList instead of a ComboBox?

               

              <s:DropDownList id="combo1" labelField="label" dataProvider="{myArray1}" x="264" y="73"/>

               

              I tested this out and it worked, I think.

               

              Chris

              1 person found this helpful
              • 4. Re: Problem about changing dataprovider of Combobx
                ejchen77 Level 1

                Hi Chris,

                 

                Thanks! It really worked!

                 

                Best Regards,

                Leon