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.
Thanks for this example. Here it is with live data from my blog (http://www.swphoto.net/advgrid/).
For example aggregated price/cost per mediatype. Also, display the number of sub items of a group in the AdvancedDataGrid could be of interest. Typical use would be accounting sheets, where you could drill down in the data. But then you'd need the summarised totals on the top level, not just the leaf nodes.
So far I haven't seen any documentation or examples showing this feature, which I'd thought would have been the main purpose for the AdvancedDataGrid control.
--- Ben
--- Ben
Don
I noticed this example is for remoteobject. How does one use do grouping for httpservice? I totally think Flex 3 (datagrid) is awesome with its perfomance (7000+ rows brought back and rendered in 4 - 6 seconds) with httpservice.
seeing passing data section here
http://livedocs.adobe.com/flex/3/html/help.html?co...
import mx.collections.ArrayCollection;
[Bindable]
public var myDP:ArrayCollection;
...
<mx:GroupingCollection id="gc" source="{myDP}">
I just don't have any sample code on converting the httpservice result to an ArrayCollection. XML to XMLllistCollection, yes, but not an ArrayCollection. Sorry.
Don
http://flexonblog.wordpress.com/2007/12/29/read-da...
forgive me that it is a php service example.:) I only have CF example going to a XMLlistCollection, but it is basically the same thing. (httpservice result to arraycollection to groupingCollection dataProvider ).
I'm sure Ben has a better example.
Don
1) Nested mx:Repeater via XMLListCollection,
2) AdvancedDataGrid Hierarchy via HierarchicalData (XML hierarchical data)
3) AdvancedDataGrid Grouping (flat data grouped - could be flat query result from CFC )
http://www.fusionpage.com/flex/demos/bin/demos.htm...
Don