InsideRIA.com is an online community dedicated to all things RIA. InsideRIA.com is developed by O'Reilly and sponsored by Adobe Systems Incorporated.
I am on my way down to Orlando for tonight's Flex / AIR Pre-Release Tour presentation to the Adobe Developers of Greater Orlando group. The full tour location list is online.
The enhancements to the data grids in Flex 3 have been eagerly anticipated, and are greatly appreciated - especially by us ColdFusion developers who spend so much time creating highly data-centric applications. I plan on providing examples of how to use the new data grids with ColdFusion, and here is the first.
AdvancedDataGrid supports group expanding and collapsing within grids, think of it as a grid with an embedded tree control. Multiple levels of grouping are supported (each level becomes a tree branch), but this example uses a single level for simplicity's sake.
The example uses the default database tables that come with ColdFusion 8, and here is the simple CFC method which just returns several columns from two tables:
<cfcomponent>
<cffunction name="getArtByMedia" access="remote" returntype="query">
<cfset var result="">
<cfquery datasource="cfartgallery" name="result">
SELECT mediatype, artname, description, price
FROM media, art
WHERE media.mediaid=art.mediaid
ORDER BY mediatype, artname
</cfquery>
<cfreturn result>
</cffunction>
</cfcomponent>
I saved this component as art.cfc in a folder named test beneath the web root. You can save it wherever you like, but of course you'll need to adjust the path in the MXML accordingly. Here is the MXML:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical" width="100%" height="100%"
creationComplete="initApp()">
<mx:Script>
<![CDATA[
// Init
public function initApp():void
{
// Get data
svc.getArtByMedia();
}
]]>
</mx:Script>
<!-- Define CFC -->
<mx:RemoteObject id="svc"
destination="ColdFusion" source="test.Art"
result="gc.refresh()" />
<!-- The grid -->
<mx:AdvancedDataGrid width="100%" height="100%">
<!-- Define grouping in dataProvider -->
<mx:dataProvider>
<mx:GroupingCollection id="gc" source="{svc.getArtByMedia.lastResult}">
<mx:Grouping>
<mx:GroupingField name="MEDIATYPE" />
</mx:Grouping>
</mx:GroupingCollection>
</mx:dataProvider>
<!-- The grid columns -->
<mx:columns>
<mx:AdvancedDataGridColumn dataField="MEDIATYPE"
headerText="Media"/>
<mx:AdvancedDataGridColumn dataField="ARTNAME"
headerText="Name" />
<mx:AdvancedDataGridColumn dataField="DESCRIPTION"
headerText="Description" />
<mx:AdvancedDataGridColumn dataField="PRICE"
headerText="Price" textAlign="right" />
</mx:columns>
</mx:AdvancedDataGrid>
</mx:Application>
<mx:RemoteObject> points to the CFC. <mx:AdvancedDataGrid> defines the grid and columns, in much the same way as regular <mx:DataGrid> does. But unlike <mx:DataGrid>, here the dataProvider is passed as a child tag so as to be able to specify additional information. <mx:AdvancedDataGrid> can accept hierarchical data ready to be used for grouping, as well as flat data, in which case a GroupingCollection must be used so that Flex can regroup the data hierarchically. As ColdFusion queries are flat (non-hierarchical), a GroupingCollection is defined using <mx:GroupingCollection>. The GroupingCollection accepts one or more fields to be grouped on, and here a single field is specified using <mx:GroupingField>.
When using GroupingCollections like this, the data grid itself must explicitly be refreshed so that the changes are reflected in the displayed grid. According to the documentation, this example would do this as <mx:AdvancedDataGrid ... initialize="gc.refresh()">, but this does not seem to work. Instead, the refresh() method is specified in <mx:RemoteObject> so that it is called when data is received. (Thanks to Mike Nimer for figuring this one out for me!).
And there you have it, a really simple data grid with expand/collapse grouping.