4 Replies Latest reply on Jan 14, 2010 6:46 AM by sebastiantoro84

    ComboBox inside an ItemRenderer keeps going back to initial selectedIndex

    sebastiantoro84

      Hi,

           Thanks for taking a minute to help me solve this problem. I have the following component, what I want to do, is to save the selectedIndex on the data.selectedIndice variable. I have no trouble with that. The variable saves the correct new selected Index number, but the problem is that, for instance, if the initial selectedIndex was 0, and the user selects the third item of the ComboBox, the ComboBox will go back to selecting the 0 index (the new one was 3) even though that the seleccion.selectedIndex shows that 3 is selected (that's why data.selectedIndice saves 3)

      <mx:HorizontalList
              bottom="0"
              backgroundAlpha="0.0"
              id="catalogoOpciones"
              columnCount="3"
              height="80"
              hideEffect="{esconderse}"
              horizontalScrollPolicy="on"
              selectable="false"
              showEffect="{mostrarse}"
              width="100%">
              <mx:itemRenderer>
                 <mx:Component>
                     <mx:VBox  height="100%" verticalGap="0" horizontalAlign="center" creationComplete="init()">
                         <mx:Label id="titulo" text="{data.nombre}"  textAlign="center" styleName="nombreOpcionDetallePlato" />
                         <mx:ComboBox id="seleccion" dataProvider="{data.opciones}" labelField="label"                          labelFunction="funcionLabelComboBox"  styleName="comboOpcionDetallePlato" change="changeHandler(event)" />
                          <mx:Script>
                              <![CDATA[
                                  import menucomponents.clases.OpcionesEvent;

                                  import mx.events.ListEvent;
                                  import mx.controls.Alert;                                                       
                                 
                                  public function init():void {
                                  }
                                 
                                  public function changeHandler(event:ListEvent):void {        

                                         Alert.show(seleccion.selectedIndex.toString);               //this shows the correct new index selected                                                             data.selectedIndice = seleccion.selectedIndex;               //at the beginning data.selectedIndice was 0, and when I                                                                                                                          change the selectedIdex, data.selectedIndice saves  1                             }                                                                                             correct value, but the ComboBox shows on the screen that                                                                                                                          the selected item is the first one (index 0) and not the                                                                                                                          second one (index 1) AS IT'S SUPPOSED TO                           
                                 
                                  public function funcionLabelComboBox(item:Object):String {
                                          return item.label + ", $" + item.precio;
                                  }
                              ]]>
                          </mx:Script>
                      </mx:VBox>               
                  </mx:Component>
             </mx:itemRenderer>
          </mx:HorizontalList>

       

      Thanks for the help

       

      Sebastián Toro O

        • 1. Re: ComboBox inside an ItemRenderer keeps going back to initial selectedIndex
          Sebastien V. Level 3

          My guess would be that the itemRenderer is re-instantiated too often, and is loosing the selected index (the itemRenderer is then a new instance, so its selected index is 0).

           

          That being said, I think the simplest solution for you would be to bind the comboBox selectedIndex to the data.selectedIndice :

           

          <mx:ComboBox selectedIndex="{data.selectedIndice}" />
          

           

          This should require to mark the data.selectedIndice as [Bindable] in your class definition.

          • 2. Re: ComboBox inside an ItemRenderer keeps going back to initial selectedIndex
            sebastiantoro84 Level 1

            Hi NoNick4Me, thanks for your help. The class where the ItemRendered gets its data is [Bindable], and I did what you told me but the problem is still there. Do you have another solution?

             

            Thanks a lot

             

            Sebastián Toro O,

            • 3. Re: ComboBox inside an ItemRenderer keeps going back to initial selectedIndex
              Sebastien V. Level 3

              Check that the selectedIndice property is public or has both setter and getter, so that the [Bindable] class tag applies to the selectedIndice property.

               

              If that still doesn't work, try to override the component data setter : (edit: the setter is the Component setter, not the ComboBox one)

               

              override public function set data( value:Object ):void {
                   super.data = value;
                   this.selectedIndex = data.selectedIndice;
              }
              

               

              That's all I can think of for now. Hope this helps...

              • 4. Re: ComboBox inside an ItemRenderer keeps going back to initial selectedIndex
                sebastiantoro84 Level 1

                selectedIndice is public. What's funny is that if I don't use the selectedIndex="{data.selectedIndice}" and I comment the line:

                 

                data.selectedIndice = seleccion.selectedIndex

                 

                Then the CombBox works just fine. It doesn't go back to the initial selectedIndex. I´m going to solve this using Events and an external Array (Array.length = ComboBox.dataProvider.length) to save the selectedIndex, it's a very un-elegant solution but i think it's going to work. Also I might add, that if I send an Event and catch it outside the List, and try to modify the data.selectedIndice, from the particular ComboBox that I changed, the ComboBox still changes automatically it's selectedIndex.

                 

                Thanks for your help.

                 

                If some day you think how to solve this, I would really appreciate if you post it here.

                 

                Sebastián Toro O.