Search Flex Components Free

Custom Search

December 27, 2007

Flex Data Collections Sorting (AS) Sample Source Code:

Creat a File:
Sortexampleas.mxml

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

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="init();">

<mx:Script>
<![CDATA[

import mx.collections.*;

private function init():void
{
var xmlCollection:XMLListCollection = new XMLListCollection(complexData);
var sort:Sort = new Sort();
sort.fields = [new SortField("@first")];
xmlCollection.sort = sort;
xmlCollection.refresh();
xmlDG.dataProvider = xmlCollection;
}

private function setPhone(item:Object, dataField:Object):String
{
return String(item.contact.phone);
}

private var complexData:XMLList = <>
<person first="John" last="Doe" id="104" >
<contact>
<phone>415-832-7575</phone>
<fax>415-596-3699</fax>
</contact>
</person>
<person first="Jane" last="Smith" id="108" >
<contact>
<phone>415-832-1234</phone>
<fax>415-678-0877</fax>
</contact>
</person>
<person first="Ellen" last="Jones" id="116" >
<contact>
<phone>415-832-0987</phone>
<fax>415-357-4578</fax>
</contact>
</person>
</>;

]]>
</mx:Script>

<mx:Label text="The DataGrid should be sorted by first name." />

<mx:DataGrid id="xmlDG">
<mx:columns>
<mx:DataGridColumn dataField="@first" headerText="First"/>
<mx:DataGridColumn dataField="@last" headerText="Last"/>
<mx:DataGridColumn labelFunction="setPhone" headerText="Phone Number"/>
</mx:columns>
</mx:DataGrid>

</mx:Application>

Related Flex Tutorials