Skip navigation
Currently Being Moderated

Set specific selectedItem in DropDownList

Jun 26, 2010 12:00 PM

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!

 
Replies
  • Currently Being Moderated
    Jun 26, 2010 2:44 PM   in reply to Francisc

    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]

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 6, 2010 7:42 AM   in reply to rootsounds

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 6, 2010 7:51 AM   in reply to RobX82

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

     
    |
    Mark as:
  • rtalton
    1,133 posts
    Oct 28, 2008
    Currently Being Moderated
    Jul 6, 2010 8:37 AM   in reply to Francisc

    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>

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 6, 2010 9:47 AM   in reply to Francisc

    MASH,

     

    Did you make a custom spark dropdownlist component for this?

     
    |
    Mark as:
  • Currently Being Moderated
    May 12, 2013 1:11 AM   in reply to rtalton

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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points