6 Replies Latest reply on Apr 28, 2011 11:21 AM by Flex harUI

    How to select items with selectedItem and selectedIndex?

    pbesi

      Hello.

       

      I'm trying to selected and item in both a dropdownlist and combobox using selectedItem or selectedIndex properties. They don't work

      Browing internet I found a lot of people with the same problem, that was already know in 2008.

       

      The easy commands ddcompontent.selectedIndex = value or ddcomponent.selectedItem = item don't work.

      Can anyone help me?

       

      I couldn't find any solution.

       

      thank you

      Pbesi

        • 1. Re: How to select items with selectedItem and selectedIndex?
          Flex harUI Adobe Employee

          Post a 20-line test case.

          • 2. Re: How to select items with selectedItem and selectedIndex?
            ssardar

            Without seeing your code it's hard to tell where you are going wrong.

             

            I have comboboxes which I need to set to the last value selected.

             

            I wrote a function like this, and it works fine for my needs.  Given you know the value you want to select.  if you know the index of the item already then you should just be able to set that.

             

             

             

             

             

            private function setCmb(cbx:ComboBox, val:String):void

            {

             

             

             

            var arrC:ArrayCollection = cbx.dataProvider as ArrayCollection;

             

            for (var i: int = 0; i < arrC.length; i++) {

             

            var pe:String = arrC.getItemAt(i) as String;

             

            if ( val == pe) {

            cbx.selectedIndex = i;

             

            break;

            }

             

             

            }

             

            1 person found this helpful
            • 3. Re: How to select items with selectedItem and selectedIndex?
              pbesi Level 1

              Thank you Flex harUI and ssadar.

               

              I write here some pieces of my code:

               

              <s:RemoteObject id="tipoTitoloService" destination="TipoTitoloService" endpoint="http://localhost:8080/Rubrica/messagebroker/amf">

                     <s:method name="loadAllTipoTitolo"/>

              </s:RemoteObject>

               

               

               

              protected function tipoTitoloDD_creationCompleteHandler(event:FlexEvent):void

              {

                    tipoTitoloService.loadAllTipoTitolo();

              }

               

               

              Here I try to select the third item, but it does't work. Anyway, cliccking the dropdownlist, I can see all the items coorectly loaded.

               

              <s:DropDownList id="tipoTitoloDD"

                   creationComplete="tipoTitoloDD_creationCompleteHandler(event)"

                   dataProvider="{tipoTitoloService.loadAllTipoTitolo.lastResult}"

                   selectedIndex="3"

                   labelField="des"/>

               

              The same problem if I try to select an item with selectedItem property.

               

              I also tryed to work with actionscript code. If I try to define a "click" event and I write the code

               

              private function click_handler(event:MouseEvent): void {

               

              titpoTitoloDD.selectedIndex=3;

               

              }

               

              I can't solve the problem.

               

              The code suggested by ssardar works fine.

              So, I think.... Is it possible that the selection is performed before the items are loaded by remote?

               

              Any other suggestion?

              Thank you

               

              pbesi

              • 4. Re: How to select items with selectedItem and selectedIndex?
                Flex harUI Adobe Employee

                Yes, loading is asynchronous, you the selectedIndex is applied before the

                dataprovider is updated.

                 

                I would not use binding and use the result event to set the dataProvider,

                then set the selectedIndex.  You may need to call validateNow before setting

                selectedIndex

                • 5. Re: How to select items with selectedItem and selectedIndex?
                  pbesi Level 1

                  Hello.

                   

                  Ok, part of problem has been solved. Thank you. Now the property selectedIndex works correctly.

                  But I need to use selectedItem and it doesn't work yet.

                   

                  Here my last code:

                   

                  <s:RemoteObject id="tipoTitoloService" destination="TipoTitoloService" endpoint="http://localhost:8080/Rubrica/messagebroker/amf">

                  <s:method name="loadAllTipoTitolo" result="loadAllTipoTitolo_Hanlder(event)"/>

                  </s:RemoteObject>

                   

                  protected function tipoTitoloDD_creationCompleteHandler(event:FlexEvent):void

                  {

                       tipoTitoloService.loadAllTipoTitolo();

                  }

                   

                   

                  private function loadAllTipoTitolo_Hanlder(event:ResultEvent):void

                  {

                       tipoTitoloDD.dataProvider = tipoTitoloService.loadAllTipoTitolo.lastResult;

                       tipoTitoloDD.selectedItem = persona.tipoTitolo as TipoTitolo;          // tipoTitoloDD.selectedIndex = 4. It works!!

                       Alert.show(persona.tipoTitolo.des);

                       tipoTitoloDD.validateNow();

                  }

                   

                  <s:DropDownList id="tipoTitoloDD"

                       creationComplete="tipoTitoloDD_creationCompleteHandler(event)"

                  labelField="des"/>

                   

                  Well, as I wrote above, the selectedIndex works fine. Now in according with the code above, the selectedItem doesn't work.

                  Please, notice that I wrote an Alert command and it confirms that the object property persona.tipoTitolo.des is correct.

                  At the same time, the dropdownlist has objects of the same type (TipoTitolo).

                  So, I cannot understand why it doesn't select the right item.

                   

                  Can you help me?

                  thank you

                   

                  Pbesi

                  • 6. Re: How to select items with selectedItem and selectedIndex?
                    Flex harUI Adobe Employee

                    The selectedItem must be the same instance that is in the dataProvider.  It

                    can't be another instance with the same properties.