如何在没有循环的情况下从flex中的arraycollection获取特定项目

时间:2012-11-27 10:20:53

标签: flex arraycollection

例如:我有一个包含名称,标准,年龄等的arraycollection。 我只想要那些标准'x'的学生。 如何在没有循环的flex ArrayCollection中实现这一点。

1 个答案:

答案 0 :(得分:0)

下面的代码可以帮助您: - 您可以将filterFunction用于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>