8 Replies Latest reply on May 12, 2013 1:11 AM by Tony Mohapatra

    Set specific selectedItem in DropDownList

    Francisc Level 3

      Hello!

       

      I have an ArrayCollection with objects as dataProvider for a DropDownList and a previously selected value.

      How can I set the DropDownList to that previously selected value without running a loop in the dataProvider getting the index of the object I want.

       

      Example:

       

      The data provider:

      ArrayCollection([{id:1,name:"George"},{id:5,name:"Mary"},{id:10,name:"Rooney"}]);

       

      The value:

      selectedID=10;

       

       

      How can I set the DropDownList to Rooney (ID 10)?

      I can run a for loop, but surely there is a more elegant way?

       

      Thank you!

        • 1. Re: Set specific selectedItem in DropDownList
          rootsounds Level 4

          None that I can think of since the data provider update likely carries brand new objects. Whatever you do to search through an ArrayCollection is going to be a linear operation that requires a loop.

           

          If your dataProvider was an XML object, you could use an E4X expression to filter it in 1 line.

          xmlProvider.(@id == previousSelectionId)[0]

          • 2. Re: Set specific selectedItem in DropDownList
            RobX82

            I am in the same situation as MASH currently.  I want to set the selected value for a dropdown in a clean manner and not have to do a loop.  There should be a field in the component called "DATA" that you assign what field in the return set it is tied too.  Then you could just have something to set the dropdown by that value.  Name/Value pairs should be better supported in Flex.

            • 3. Re: Set specific selectedItem in DropDownList
              rootsounds Level 4

              Agreed. One could certainly extend the controls to provide this functionality though.

              • 4. Re: Set specific selectedItem in DropDownList
                Francisc Level 3

                I found a way, but it's less elegant that I would have wanted...

                 

                I do this:

                 

                for(var i:uint=0;i<theDataProvider.length;i++)

                {

                     if(theDataProvider[i].id==previouslySelectedID)

                     {

                          myDropDownList.selectedItem=theDataProvider[i];

                          break;

                     }

                }

                • 5. Re: Set specific selectedItem in DropDownList
                  rtalton Level 4

                  Mornin' MASH,

                  You might want to look into using a cursor on the data provider when you know what you are looking for. This way you can avoid a loop.

                   

                  Here's a working sample you can experiment with. Note that the data must be sorted first on the field of interest before you can search through it.

                  So you just sort, search, then select.

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
                      creationComplete="doSort()" >

                   

                  <mx:Script>
                      <![CDATA[
                          import mx.collections.IViewCursor;
                          import mx.collections.Sort;
                          import mx.collections.SortField;
                          import mx.collections.ArrayCollection;
                         
                          private var cursor:IViewCursor;
                                 
                          [Bindable]
                          private var ac:ArrayCollection = new ArrayCollection([
                          {id:5,name:"Mary"},
                          {id:10,name:"George"},
                          {id:1,name:"Rooney"}
                          ]) ;
                         
                          private function doSort():void{
                              var sf:SortField = new SortField('id',false,false,true);
                              var s:Sort = new Sort();
                              s.fields = [sf];
                              ac.sort = s;
                              ac.refresh();
                          }
                         
                          private function handleClick():void{
                              var obj:Object = {id:10}
                              cursor = ac.createCursor();
                              var found:Boolean = cursor.findAny(obj);
                              if(found){
                                  cb.selectedItem = cursor.current;
                              }
                          }
                         
                      ]]>
                  </mx:Script>
                      <mx:ComboBox id="cb" labelField="name" dataProvider="{ac}" x="84" y="88"></mx:ComboBox>
                     
                      <mx:Button click="handleClick()" x="245" y="87" label="Select id:10"/>
                     
                  </mx:Application>

                  • 6. Re: Set specific selectedItem in DropDownList
                    RobX82 Level 1

                    MASH,

                     

                    Did you make a custom spark dropdownlist component for this?

                    • 7. Re: Set specific selectedItem in DropDownList
                      Francisc Level 3

                      Nope, "my method" works with the default DDL.

                      • 8. Re: Set specific selectedItem in DropDownList
                        Tony Mohapatra

                        I hav tried with auto sorting in ascending order but it's not working.