Quick Tip: Flex ArrayCollection filter in-line

A quick tip for those of you who want to do some ArrayCollection filtering, for let’s say duplicates. Rather than have two methods, do it all in-line:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white" creationComplete="init()">
 
    <mx:Script>
        <![CDATA[
import mx.collections.SortField;
import mx.collections.Sort;
import mx.collections.ArrayCollection;
[Bindable]
private var catFamilyAC:ArrayCollection;
[inject]private var model:ModelController;
private function init():void {
var cat:CATDTO;
/* Initialize and populate the ArrayCollection object. */
catFamilyAC = new ArrayCollection();
for each (var cat:CatDTO in model.otherArraySourceAC) {
catFamilyAC.addItem(cat);
}
}

...
/** As you can see, inline, I have added a function rather than having to add another function to link it up to */
/** this one filters for duplicate cat_ids **/
private function filterAC():void {
catFamilyAC.filterFunction = function(item:CatDTO):Boolean{
return cats[item.CAT_ID] ? true : false;
}
}
...
]]>
    </mx:Script>
 ...
 
</mx:Application>


Leave a Comment