例如:我有一个数组集合,其中包含名称,标准,年龄等。我只想要标准"x"中的学生计数。如何在没有循环的 flex ArrayCollection 中做到这一点。
下面的代码可能会帮助你: - 您可以使用 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>