0

For eg: I have an arraycollection which contains name,standard,age etc. I just want the count of students who were in standard 'x'. How this can be possible in flex ArrayCollection without a loop.

1 Answers1

0

Below code may help You: - you can use filterFunction for ArrayCollection.

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" xmlns:local="*">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;

            private var filterValue:String = "X"; 
            private var myArrayCollection:ArrayCollection = new ArrayCollection(
                   [{label:"I", data:"first std"},
                    {label:"II", data:"Second  std"},
                    {label:"III", data:"Third  std"},
                    {label:"V", data:"Fifth  std"},
                    {label:"VI", data:"Sixth  std"},
                    {label:"IX", data:"Ninth  std 1"},
                    {label:"X", data:"Tenth  std 1"},
                    {label:"IX", data:"Ninth  std 2"},
                    {label:"X", data:"Tenth  std 2"},
                    {label:"X", data:"Tenth  std 3"}]
            );

            private function getLengthOfArrayCollection():void
            {
                myArrayCollection.filterFunction = filterFunctionHandler;
                myArrayCollection.refresh();
                trace(myArrayCollection.length+" --")
            }

            private function filterFunctionHandler(item:Object):Boolean 
            {
                var value:Boolean = false;
                if(item.label == filterValue)
                {
                    value = true;
                }
                return value;
            }
        ]]>
    </fx:Script>
    <s:layout>
        <s:HorizontalLayout />
    </s:layout>
    <s:Button label="Click for Filter" click="getLengthOfArrayCollection()" skinClass="MyButtonSkinClass"/>
</s:Application>
Mahesh Parate
  • 786
  • 1
  • 4
  • 13
  • Well, this still uses a loop; the ArrayCollection just abstracts the loop so you don't have to write it yourself. It may be worth nothing that this approach would change the visual display of any list based classes using that ArrayCollection as their dataProvider. – JeffryHouser Nov 27 '12 at 16:31
  • 1
    Hi guys Thanks for your replies. I got another idea. Handling the data with XML. Its very easy and got it right what I want. – Vishnu Vikraman Sreekala Nov 28 '12 at 10:32