ArrayCollection with multiple filter functions
Lately I have been working a lot with complex forms and data filters. If you have some four or five forms, each affecting the same data collection and each having between 5 and 10 filter criteria, you'll start feeling you're no longer in control of your situation.
The ArrayCollection class has a built-in filtering mechanism, where you pass a function reference to the filterFunction property in order to filter out the items that don't satisfy the function's criteria. This is extremely useful, but if you need to filter on more than one criterion you'll have to complicate the filter function more and more. I don't know why, but It gives me the same chills on my spine as hardcoding does.
The solution was to extend the ArrayCollection and add the option to provide an array of function references as filters. The final result is the AND between each evaluated filter function. It's one of those situations when writing some few extra lines of code really pays off.
Here's the class code:
package eu.rotundu.collections { import mx.collections.ArrayCollection; public class ArrayCollectionExtended extends ArrayCollection { private var _filterFunctions:Array; public function ArrayCollectionExtended( source:Array = null ) { super(source); } public function set filterFunctions( filtersArray:Array ):void { _filterFunctions = filtersArray; this.filterFunction = complexFilter; } public function get filterFunctions():Array { return _filterFunctions; } protected function complexFilter( item:Object ):Boolean { var filterFlag:Boolean = true; var filter:Function; for each(filter in filterFunctions) { filterFlag = filter( item ); if( !filterFlag ) break; } return filterFlag; } } }
And here's how you use it:
someArrayCollection.filterFunctions = [ filterByVendor, filterByPrice, filterByTime, filterByEventType, filterByKeywords ]; someArrayCollection.refresh();
You can download the source code.
You can also run a working example or view its source code.



That’s very useful and posted just at the right time … thank’s