Many times developers want to apply filtering functions to an ArrayCollection. However, there might be several components or functions which use this very same ArrayCollection for data binding. When this is the case, and a filtering function is applied to the ArrayCollection, all components bind to the ArrayCollection reflect the filtering, which might not be something the developer wants or expects to happen.
The reason why this happens is because the ArrayCollection acts as a wrapper to its source Array. When a filtering function is applied to the ArrayCollection, the filtered out objects are hidden by the wrapper. However, all original objects can still be accessed by using the ArrayCollection’s source array.
The following example illustrates the issue. There is an ArrayCollection, itemsAC, to which we apply the filter function “showOnlyFruits”.
[Bindable]
/*
* DataProvider for the combobox
*
* @public
*/
public var itemsAC:ArrayCollection =
new ArrayCollection([{label: "banana", type: "fruit"},
{label: "mango", type: "fruit"},
{label: "lettuce", type: "vegetable"},
{label: "spinach", type: "vegetable"}]);
private function toggleItemsVisibility():void{
// only show fruits
itemsAC.filterFunction = showOnlyFruits;
itemsAC.refresh();
}
private function showOnlyFruits(p_item:Object):Boolean
{
return (p_item.type == "fruit") ? true : false;
}
After the filter function gets executed, we can check the length of both the ArrayColeciton and its source.
trace(this.itemsAC.length); //2
trace(this.itemsAC.source.length); //4
As you can notice, the ArrayCollection only has 2 items, the filtered ones. But the source array has all of the original items available.