Search code examples
apache-flexflex4flex4.5flex-spark

Looping through Form children in Flex 4


I have a spark form that looks this. I am trying to loop through the form and push the id of each individual DropDownList into an array.

I am able trace out the ids of the form items successfully. But I cannot get the id of DropDownList using any of the methods that for accessing children that I used is Flex 3. I am new Flex 4 and after some reading I understood its due to something related to the spark architecture.

This is form.

<s:Form id="facilities" x="51" y="32" width="595" height="402">

        <s:FormHeading label="SWOT ANALYSIS FORMAT FOR PLOT PURCHASE" fontSize="15">

        </s:FormHeading>

        <s:FormItem id = "petrolPumpsFormItem" required="true" width="464" label="Petrol Pumps:">
            <s:DropDownList id = "petrolPumps" width="220"  prompt="Select distance from the plot" labelField="distance" >
                <s:dataProvider>
                    <s:ArrayCollection>

                        <fx:Object distance="1-3 km"   mark="100"/>
                        <fx:Object distance="3-6 km"   mark="90"/>
                        <fx:Object distance="6-9 km"   mark="80"/>
                        <fx:Object distance="9-12 km"  mark="70"/>
                        <fx:Object distance="12-15 km" mark="60"/>
                        <fx:Object distance="15-18 km" mark="50"/>

                    </s:ArrayCollection>
                </s:dataProvider>
            </s:DropDownList>
        </s:FormItem>

        <s:FormItem id="filmTheatreFormItem" required="true" width="464" label="Film Theatre:">
            <s:DropDownList id="filmTheatre" width="220"  prompt="Select distance from the plot" labelField="distance" >
                <s:dataProvider>
                    <s:ArrayCollection>

                        <fx:Object distance="1-3 km"   mark="100"/>
                        <fx:Object distance="3-6 km"   mark="90"/>
                        <fx:Object distance="6-9 km"   mark="80"/>
                        <fx:Object distance="9-12 km"  mark="70"/>
                        <fx:Object distance="12-15 km" mark="60"/>
                        <fx:Object distance="15-18 km" mark="50"/>

                    </s:ArrayCollection>
                </s:dataProvider>
            </s:DropDownList>
        </s:FormItem>

        <s:FormItem id= "atmFormItem" required="true" width="464" label="ATM:">
            <s:DropDownList id= "atm" width="220"  prompt="Select distance from the plot" labelField="distance" >
                <s:dataProvider>
                    <s:ArrayCollection>

                        <fx:Object distance="1-3 km"   mark="100"/>
                        <fx:Object distance="3-6 km"   mark="90"/>
                        <fx:Object distance="6-9 km"   mark="80"/>
                        <fx:Object distance="9-12 km"  mark="70"/>
                        <fx:Object distance="12-15 km" mark="60"/>
                        <fx:Object distance="15-18 km" mark="50"/>

                    </s:ArrayCollection>
                </s:dataProvider>
            </s:DropDownList>
        </s:FormItem>

    </s:Form>

I could get acess to the Form item like this. 'facilities' is the name of the Form.

for (var i:int = 0 ;i <= facilities.numElements-1;i++)
    {
    var item:IVisualElementContainer = facilities.getElementAt(i) as IVisualElementContainer;
    trace(item);
    }

I tried this link http://www.igorcosta.org/?p=366 and tried to use the class given there.

The problem was that an error was thrown. Flash builder took me to the second for loop in the second function inside that code and showed that item.numElements used there was creating the problem. I tried to use item.numElements inside my code(the one shown above with which I traced out FormItem ids) and it was also not working. Anybody got any clue?


Solution

  • In your for loop, facilities.getElementAt(i) will return an IVisualElement, not necessarily an IVisualElementContainer.

    Try going with:

    var item:IVisualElement = facilities.getElementAt(i);
    trace(item);
    var itemContainer:IVisualElementContainer = item as as IVisualElementContainer;
    
    if (itemContainer) {
        trace(itemContainer.numElements);
    }
    

    That should give you the results you're looking for!