Search Flex Components Free

Custom Search

December 27, 2007

Flex Data Collections Filter Functions With ArrayCollections Sample Source Code:

Create a new file:-

filterexamples.mxml

<?xml version="1.0" encoding="iso-8859-1"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="srv.send();"

backgroundAlpha="0" >

<mx:Script>
<![CDATA[

import mx.collections.*;

private var originalCollection:ArrayCollection;
private var filteredCollection:ArrayCollection;

private function initList() : void
{
originalCollection = new ArrayCollection(srv.lastResult.airports.location);
filteredCollection = new ArrayCollection(srv.lastResult.airports.location);
originalGrid.dataProvider=originalCollection;
filteredGrid.dataProvider=filteredCollection;
filteredCollection.filterFunction = filterFunc;
filteredCollection.refresh();
}

private function filterFunc(item:Object):Boolean
{
return item.name >= "Boston" && item.name <= "Houston";
}

]]>
</mx:Script>

<mx:HTTPService id="srv" url="../assets/airports.xml" result="initList()"/>

<mx:Label text="A filterFunction is used with an ArrayCollection." />

<mx:HBox>
<mx:VBox>
<mx:Label text="DataGrid with original data." />
<mx:DataGrid id="originalGrid" />
</mx:VBox>
<mx:VBox>
<mx:Label text="DataGrid with filtered data." />
<mx:DataGrid id="filteredGrid" />
</mx:VBox>
</mx:HBox>

</mx:Application>

airpots.xml
<?xml version="1.0" encoding="utf-8"?>

<airports>
<location>
<abbreviation>IAH</abbreviation>
<name>Houston</name>
</location>
<location>
<abbreviation>BOS</abbreviation>
<name>Boston</name>
</location>
<location>
<abbreviation>MHT</abbreviation>
<name>Houston</name>
</location>
<location>
<abbreviation>JAX</abbreviation>
<name>Jacksonville</name>
</location>
<location>
<abbreviation>CVG</abbreviation>
<name>Cincinatti</name>
</location>
<location>
<abbreviation>ATL</abbreviation>
<name>Atlanta</name>
</location>
<location>
<abbreviation>CDG</abbreviation>
<name>Paris</name>
</location>
<location>
<abbreviation>JFK</abbreviation>
<name>New York</name>
</location>
<location>
<abbreviation>LAX</abbreviation>
<name>Los Angeles</name>
</location>
<location>
<abbreviation>HND</abbreviation>
<name>Tokyo</name>
</location>

</airports>

Related Flex Tutorials