Search code examples
actionscript-3comboboxitemrenderer

How to send data in a combobox itemrender?


I want to send data to a combobox itemrenderer such that it has both dropdown data and the selectedIndex. Please note that the combobox itemrenderer is part of a List.... So if the data is arraycollection, I am trying to get it mapped correctly using list comprehension....

THE SOLUTION I IMPLEMENTED

Say the combobox data is in an ArrayCollection var d. I created a new ArrayCollection d1 such that d1 items are {d: d, dSelectedIndex: whatever_val_u_determined}

In the list, I set dataProvider = {d1}

In the itemrenderer for the above list items, I set dataProvider = {data.d} and selectedIndex = {data.dSelectedIndex}


Solution

  • itemRenderer get data from ComboBox's dataProvider. For example, if you create ComboBox like this

    <s:ComboBox itemRendererFunction="myFunction"
                labelField="lastName">
         <s:dataProvider>
             <mx:ArrayList>
                 <fx:Object firstName="Steve" lastName="Smith"/>
                 <fx:Object firstName="John" lastName="Jones"/>
                 <fx:Object firstName="Mary" lastName="Moore"/>
             </mx:ArrayList>
         </s:dataProvider>
    </s:ComboBox>
    

    then your itemRenderer will get Object with params firstName and lastName in it's data variable.

    <s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
                xmlns:s="library://ns.adobe.com/flex/spark" height="30">
    
    <fx:Script><![CDATA[
        public var country:String;
        ]]></fx:Script>
    
    <s:Label text="{data.firstName + country}"/>
    

    data.firstName will be "Steve" or "John" or "Mary"

    If you want to send additional params to itemRenderer you can use ComboBox's itemRendererFunction

    private var Rend1:ClassFactory = new ClassFactory(MyItemRenderer);
    
        private function myFunction(item:Object):ClassFactory {
            Rend1.properties = {country: "USA"};
            return Rend1;
        }
    

    And now in you itemRenderer variable "country" will be "USA"