Flex Tip: How to get all items (ignoring the filter) in an ArrayCollection

If you have an ArrayCollection with a filter function set and you loop through the items, you’ll only see the items which match the filter. The catch is sometimes you want to see them all. Initially I was doing the following.

var filterFunction:Function = arrayCollection.filterFunction;
arrayCollection.filterFunction = null;
arrayCollection.refresh();
			
for each (var obj:Object in arrayCollection)
{
	// do stuff
}
			
arrayCollection.filterFunction = filterFunction;
arrayCollection.refresh();

This worked but was pretty ugly. Here’s a nicer approach:

for each (var obj:Object in arrayCollection.source)
{
	// do stuff
}

What I keep finding in Flex is that I’m able to come up with complex code to solve simple problems but when I revisit it months later there’s usually a more straight-forward solution.

Hope this helps,
Hillel

4 thoughts on “Flex Tip: How to get all items (ignoring the filter) in an ArrayCollection”

  1. Hillel,
    Do you have the complete source code for this.I am looking for a similar example which is needed in my project.

    My code :
    (value as ArrayCollection).filterFunction = filterFunction;

    private function filterFunction(item:Object):Boolean {

    if (_matchKey == false )
    return true;
    }
    return text.length == 0 || isMatch(itemToLabel(item));

    }

  2. Hillel,
    Do you have the complete as file,which gives all the items in the arraycollection ignoring the filter. I tried using the code above as said by you, it wasn’t working.

    1. Krishna,

      I’m sorry, there’s no as file. The post is just pointing out that ArrayCollections have a property called “source” which gives you access to the raw array (without any of the ArrayCollection’s filters).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s