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.
Asked
Active
Viewed 1,575 times
0
-
1I don't think there is way. What makes you ask this? – JeffryHouser Nov 27 '12 at 16:31
-
This sounds strange. To find something in an array you need to loop. Or if you load data from database just return an extra resultset with the count of your condition. – Adrian Pirvulescu Jan 27 '15 at 12:00
1 Answers
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
-
1Hi 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