AS3 Array Collection Filter

Tags: Flex
Word Count: 231

In my last article we looked Arrays & Array Collections. In this article I would like to look at how we can filter data from that Array Collection. In my last article we talked about how Array Collections can be created and how they are automatically created for us when we return a query from ColdFusion. Now its time to actually do something with that data.

The Array Collection class in AS3 is a part of the Collections package. One of the great features of a collection is If the data provider is a collection, the components are updated immediately after the collection change occurs. If we think about this in the web world we usually have to query the database with a new query that includes our filter. In Flex all we need to do is assign the filter function to our collection and refresh it. This will walk through a quick example of how to do that.

The following code may seem like a a lot at first but just follow with me. The first thing we do is create our employees Array Collection. We want to make that bindable, this means that we will be able to use that object in our data grid component. Next we will set up a data grid and toggle button bar. In the data grid we set the data provider to the employees variable that we created above and filled in our init method. The toggle button bar will call the method filterData every time an item on the bar is clicked. When the filterData method is called we are checking to see what the label of the button is. If the user clicks Managers Only we are saying assign the filter function filterManagers to our array collection and refresh the data. A filter function is a function that the view will use to eliminate items that do not match the function's criteria. All we are saying is return anyone where isManager is true.

When you use an Array Collection (or any collection for that matter) you can do some really cool stuff with it. In the next example I will show you another cool trick I learned. You can click on the image below to view a live example of this demo. The full source code is also below.


Comments

#1 Posted By: Peter Posted On: 6/23/08 7:27 PM
Hello. I want to edit data in dataGrid via textInputs. I noticed that after filtering it, when I use arrayCol.setItemAt(data from textInputs, dataGrid.selectedIndex) the CollectionEventKind is first REMOVE and than ADD, not UPDATE (OR REPLACE). The selectedIndex of datagrid after editing data is -1. This doesn't happen when there is no filter applied. Why and how to solve it? Thanks.
#2 Posted By: Steve Posted On: 10/28/09 5:43 PM
Thank you for the very clear explanation. I got filtering wrking on my app done in about 15 min,.


Post Your Comment

Leave this field empty







Show Captcha

If you subscribe, any new posts to this thread will be sent to your email address.

Copyright © 2007 Dan Vega | BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.