Monday, September 08, 2008    
Home My Books Blog ColdFusion About Me Back    

Calendar
<< Jan 2008 >>
S M T W T F S
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

Search

Categories
 • Acrobat (2) [RSS]
 • Adobe (67) [RSS]
 • AdobeMAX06 (45) [RSS]
 • AdobeMAX07 (59) [RSS]
 • AdobeMAX08 (22) [RSS]
 • AIR (126) [RSS]
 • Appearances (118) [RSS]
 • Books (68) [RSS]
 • CFEclipse (14) [RSS]
 • ColdFusion (1143) [RSS]
 • Data Services (12) [RSS]
 • Fish Tank (2) [RSS]
 • Flash (103) [RSS]
 • Flex (365) [RSS]
 • Home Automation (3) [RSS]
 • Jobs (93) [RSS]
 • JRun (12) [RSS]
 • Labs (27) [RSS]
 • LiveCycle (21) [RSS]
 • MAX (157) [RSS]
 • Regular Expressions (12) [RSS]
 • RIA (4) [RSS]
 • SQL (37) [RSS]
 • Stuff (503) [RSS]
 • Tips (CF Studio) (80) [RSS]
 • Tips (CF) (795) [RSS]
 • Tips (Dreamweaver) (91) [RSS]
 • Tips (Flex Builder) (2) [RSS]
 • Using CF (136) [RSS]
 • Wireless (97) [RSS]

Other BLOGs
 • Charlie Arehart
 • Lee Brimelow
 • Ray Camden
 • Christophe Coenraets
 • Sean Corfield
 • Mihai Corlan
 • Cornel Creanga
 • John Dowdell
 • Danny Dura
 • Enrique Duvos
 • Steven Erat
 • Kevin Hoyt
 • Serge Jespers
 • Adam Lehman
 • Duane Nickull
 • Miti Pricope
 • Andrew Shorten
 • Ryan Stewart
 • James Ward
 • Greg Wilson
 • Full As A Goog

RSS Feeds
 • Feed
 • Subscribe

Join my mailing list and find out about new books and other topics of interest.

Thoughts, ideas, tips, musings, and pontifications (not necessarily in that order) by Ben Forta ...
NOTE: This is my personal blog, and the opinions and statements voiced here are my own.

Viewing By Entry / Main
January 29, 2008

Summarizing Grouped Data With Flex 3 AdvancedDataGrid

Last week I showed how to use the new Flex 3 AdvancedDataGrid to group data (that example used ColdFusion data returned as a query, although the example could also be applied to other back-ends, too). Grouping creates a tree within the data grid, so that groups of data may be collapsed and expanded as needed.

Now let's take the example a step further, this time adding summary data. For example, suppose you were displaying sales orders, grouped by customer and then by order number. When collapsed the tree would list all customers, but no details. When a customer was expanded the tree would list all orders for that customer, but no details. When a specific order was expanded, then the details for that order would be displayed. That's how basic grouping works.

But what if you wanted to show summary data for groups? For example, customer rows could show total of all orders for that customer, regardless of how many orders there were, and whether or not the customer group is expanded or collapsed. And then order rows could show order totals. You get the idea. And yes, this functionality is built in to the Flex 3 AdvancedDataGrid.

As before, this is a ColdFusion powered example using the default database tables that come with ColdFusion 8. Here is the simple CFC method which just returns several columns from three tables:

<cfcomponent>

   <cffunction name="getOrders" access="remote" returntype="query">
      <cfset var result="">

      <cfquery datasource="cfartgallery" name="result">
      SELECT orders.orderid,
            artname, orderitems.price,
            customerlastname || ', ' || customerfirstname AS customer
      FROM orders, orderitems, art
      WHERE orders.orderid=orderitems.orderid
         AND orderitems.artid=art.artid
      ORDER BY customer, orders.orderid
      </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. (If you tried the previous example, you could just add this getOrders() method to the existing CFC.

Now for 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.getOrders();
         }
      ]]>
   </mx:Script>


   <!-- Define CFC -->
   <mx:RemoteObject id="svc"
            destination="ColdFusion"
            source="test.Art"
            result="gc.refresh()" />


   <!-- Button bar -->
   <mx:ApplicationControlBar width="100%">
      <!-- Expand all rows in data grid -->
      <mx:Button label="Expand All"
               click="adg.expandAll()" />

      <!-- Collapse all rows in data grid -->
      <mx:Button label="Collapse All"
               click="adg.collapseAll()" />

   </mx:ApplicationControlBar>

   <!-- The grid -->
   <mx:AdvancedDataGrid id="adg"
         width="100%" height="100%">


      <!-- Define grouping in dataProvider -->
      <mx:dataProvider>
         <!-- Convert flat data to group -->
         <mx:GroupingCollection id="gc"
               source="{svc.getOrders.lastResult}">

            <mx:Grouping>
               <!-- First group by customer -->
               <mx:GroupingField name="CUSTOMER">
                  <!-- Calculate total per customer -->
                  <mx:SummaryRow summaryPlacement="group">
                     <mx:fields>
                        <mx:SummaryField dataField="PRICE"
                                    operation="SUM"
                                    label="Total"/>

                     </mx:fields>
                  </mx:SummaryRow>
               </mx:GroupingField>
               <!-- Then group by orderid -->
               <mx:GroupingField name="ORDERID">
                  <!-- Calculate total per orderid -->
                  <mx:SummaryRow summaryPlacement="group">
                     <mx:fields>
                        <mx:SummaryField dataField="PRICE"
                                    operation="SUM"
                                    label="Total"/>

                     </mx:fields>
                  </mx:SummaryRow>
               </mx:GroupingField>
            </mx:Grouping>
         </mx:GroupingCollection>
      </mx:dataProvider>

      <!-- The grid columns-->
      <mx:columns>
         <!-- Empty colume for tree -->
         <mx:AdvancedDataGridColumn />
         <!-- Query columns -->
         <mx:AdvancedDataGridColumn dataField="CUSTOMER"
            headerText="Customer" />

         <mx:AdvancedDataGridColumn dataField="ORDERID"
            headerText="Order"/>

         <mx:AdvancedDataGridColumn dataField="ARTNAME"
            headerText="Item" />

         <mx:AdvancedDataGridColumn dataField="PRICE"
            headerText="Price" textAlign="right" />

         <!-- Column for calculated totals -->
         <mx:AdvancedDataGridColumn dataField="Total"
            headerText="Total" textAlign="right" />

      </mx:columns>

   </mx:AdvancedDataGrid>

</mx:Application>

I am not going to explain the basics of invoking the CFC method via <mx:RemoteObject>, populating the GroupingCollection, and refreshing the <mx:AdvancedDataGrid>. That was covered in the previous post. Rather, let's look at what has changed.

First of all, to make it easier to expand and collapse the tree within the <mx:AdvancedDataGrid>, I added a <mx:ApplicationControlBar> containing two buttons, one to expand all rows and one to collapse all rows.

As before, <mx:GroupingField> is used to define the grouping levels. In the previous example a single level of grouping was used, here two are used (to group by customer, and then by order id within customer). In the previous example <mx:GroupingField> had no child tags, because simple grouping was used. Here, <mx:SummaryRow> has been added to each <mx:GroupingField> so as to summarize data by those groups. <mx:SummaryRow> can display summary data at the group level, as well as at the first or last member within the group, as specified by the "summaryPlacement" attribute. A <mx:SummaryRow> contains one or more fields to be summarized. Here a single <mx:SummaryField> is used for each <mx:SummaryRow> to "SUM" the total of that group, putting the result in a column named "Total". The "operation" attribute specifies the summary to be performed, and AVG, COUNT, MAX, MIN, and SUM are all built-in and ready for use. It is also possible to perform your own calculations, specifying an ActionScript function to be called instead of one of the built-in operations.

And as before, <mx:columns> is used to list the columns within the <mx:AdvancedDataGrid>. But here, two non-database columns have been added. An empty column has been inserted at the front of the list. This forces <mx:AdvancedDataGrid> to display the tree in its own column, rather than in the actual data column (and this technique can be used whether or not summary data is being displayed). In addition, a row named "Total" has been added to the end of the column list to display the summary totals. The <mx:SummaryField> tags point to the "Total" as the destination for calculated summary data. This is optional, the "Total" column could have been omitted (both from the <mx:columns> list and from the <mx:SummaryField> tags) in which case the summary values would have been displayed within the field being summarized (in this example the "PRICE" column).

As you can see, adding summary data to an <mx:AdvancedDataGrid> is not at all difficult once grouping has been implemented.

One last note, the example data used here is not ideal, as each customer has a single order, and only one order (for customer "White, Sue") has more than one item. You may want to add some data to the example tables to better demonstrate how summary data works. Or just use your own data.

Related Blog Entries

TrackBacks
Footer DataGrid - display a total or average summary footer
[...] I also tried using SummaryRow, SummaryField and GroupingCollection to do this, as Ben Forta explained. However, those really only show grouped data, while I really needed to show a summary row for flat data. [...]
Tracked by Flex and Its Environs | Tracked on 7/23/08 6:11 PM

No trackback URL. Trackbacks are only allowed via interactive form.

Comments
Ben,

Great help. Thank you!
# Posted By Steve Walker | 1/29/08 6:57 PM
Ben,
In this example, is there a way to have different operations at different levels. Say, for example, we find the total price per order and then, what was the max. order that each customer ever had. I tired doing that but the operation works on the root fields only and not on the summarized data. Could you help?
Thanks in advance,
GG
# Posted By GG | 2/7/08 10:37 PM
You can definitely do that using a custom summary function.

--- Ben
# Posted By Ben Forta | 3/3/08 12:45 PM
I am trying to build advanced grid dynamically based on results of a query. It has to be dynamic, because the #of columns would change and the headers would change based on selection of a combo box on the page. I would like to build dynamic column grouping. I need help on how I can achieve this dynamically. In what format should the structure be and how do I assign the group headers dynamicaly. I can send you the code.

Thanks a lot. Your website is heaven sent!
# Posted By Ambika | 4/21/08 10:13 PM

  © Copyright 1997-2008 Ben Forta, All Rights Reserved