ArrayCollection:具有多个过滤功能的filterFuntion

时间:2015-11-28 21:25:15

标签: actionscript-3 flex flash-builder

我使用2个条件搜索想法过滤 ArrayCollection。 我必须通过功能参数 你知道怎么做吗?

感谢您的帮助。

祝你好运

1 个答案:

答案 0 :(得分:1)

以下是使用多个过滤器函数的简单方法示例:

<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
           xmlns:s="library://ns.adobe.com/flex/spark"
    creationComplete="onCreationComplete()">

<fx:Script><![CDATA[
    import mx.collections.ArrayCollection;
    import mx.collections.ArrayList;

    private var selectedHairColor:String = "";
    private var selectedGender:String = "";
    private var selectedGrade:Number = 0;


    private var students:ArrayCollection = new ArrayCollection([
        {Name:"Bikram Dangol", HairColor:"Black", Gender:"Male", Age: 16, Grade:10},
        {Name:"Adesh Khadka", HairColor:"Black", Gender:"Male", Age: 16, Grade:10},
        {Name:"Alok Shrestha", HairColor:"Black", Gender:"Male", Age: 16, Grade:10},
        {Name:"Aaron Aryal", HairColor:"Black", Gender:"Male", Age: 14, Grade:9},
        {Name:"Babu Aryal", HairColor:"Black", Gender:"Male", Age: 15, Grade:9},
        {Name:"Bipra Dangol", HairColor:"Black", Gender:"Female", Age: 6, Grade:1},
        {Name:"Binit Dangol", HairColor:"Black", Gender:"Male", Age: 5, Grade:1},
        {Name:"Andrew Jones", HairColor:"White", Gender:"Male", Age: 17, Grade:10},
        {Name:"Justin Williams", HairColor:"White", Gender:"Male", Age: 17, Grade:10},
        {Name:"Randy Smith", HairColor:"Grey", Gender:"Female", Age: 15, Grade:9},
        {Name:"Dibya Maharjan", HairColor:"Black", Gender:"Female", Age: 14, Grade:8},
    ]);

    private var hairColors:ArrayList = new ArrayList([
        {label:"Black", data:"Black"},
        {label:"White", data:"White"},
        {label:"Gold", data:"Gold"},
        {label:"Grey", data:"Grey"},
    ]);

    private var genders:ArrayList = new ArrayList([
        {label:"Male", data:"Male"},
        {label:"Female", data:"Female"}
    ]);

    private var grades:ArrayList = new ArrayList([
        {label:"One", data:1},
        {label:"Two", data:2},
        {label:"Three", data:3},
        {label:"Four", data:4},
        {label:"Five", data:5},
        {label:"Six", data:6},
        {label:"Seven", data:7},
        {label:"Eight", data:8},
        {label:"Nine", data:9},
        {label:"Ten", data:10}
    ]);

    private var multipleFilterFunctions:Array = [hairFilterFunction, genderFilterFunction, gradeFilterFunction];

    private function hairFilterFunction(item:Object):Boolean{
        if(item.HairColor == selectedHairColor || selectedHairColor == "")
        {
            return true;
        }
        return false;
    }

    private function genderFilterFunction(item:Object):Boolean{
        if(item.Gender == selectedGender || selectedGender == "")
        {
            return true;
        }
        return false;
    }

    private function gradeFilterFunction(item:Object):Boolean{
        if(item.Grade == selectedGrade || selectedGrade == 0)
        {
            return true;
        }
        return false;
    }

    private function multipleFilterFunction(item:Object):Boolean
    {
        var filterFlag:Boolean = true;
        var filter:Function;
        for each (filter in multipleFilterFunctions)
        {
            filterFlag = filter(item);
            if (!filterFlag)
                break;
        }
        return filterFlag;
    }

    private function hairColorChangeHandler():void{
        if(cbHairColor.selectedIndex > -1 && cbHairColor.selectedItems != null)
        {
            selectedHairColor = cbHairColor.selectedItem.data;
        }
        else
        {
            selectedHairColor = "";
        }
        students.refresh();
    }
    private function genderChangeHandler():void{
        if(cbGender.selectedIndex > -1 && cbGender.selectedItems != null)
        {
            selectedGender = cbGender.selectedItem.data;
        }
        else
        {
            selectedGender = "";
        }
        students.refresh();
    }

    private function gradeChangeHandler():void{
        if(cbGrade.selectedIndex > -1 && cbGrade.selectedItems != null)
        {
            selectedGrade = cbGrade.selectedItem.data;
        }
        else
        {
            selectedGrade = 0;
        }
        students.refresh();
    }

    private function onCreationComplete():void
    {
        students.filterFunction = multipleFilterFunction;
        students.refresh();
    }

    ]]></fx:Script>
<s:Panel title="Use of Multiple Filters Example" verticalCenter="0" horizontalCenter="0"
         width="600" height="400">
    <s:layout>
        <s:VerticalLayout/>
    </s:layout>
    <s:HGroup width="100%">
        <s:Label text="Hair Color:"/>
        <s:ComboBox id="cbHairColor" dataProvider="{hairColors}" width="30%"
                    change="hairColorChangeHandler()"/>
        <s:Label text="Gender:"/>
        <s:ComboBox id="cbGender" dataProvider="{genders}" width="30%"
                    change="genderChangeHandler()"/>
        <s:Label text="Grade:"/>
        <s:ComboBox id="cbGrade" dataProvider="{grades}" width="30%"
                    change="gradeChangeHandler()"/>
    </s:HGroup>
    <s:DataGrid id="studentDataGrid" dataProvider="{students}" width="100%" height="100%"/>
</s:Panel>
</s:Application>

All Data Multiple Filtered data