Thursday, November 8, 2007

Generating summary for flat data using GroupingCollection

This example shows how GroupingCollection can be used to generate summary data for flat data and AdvancedDataGrid can be used to display the result.

The trick (or dirty work around if you like to call it so) is to generate a single dummy group using a invalid grouping field. The summary we require is specified as usual using the SummaryRow and SummaryField.

When this GroupingCollection is fed to ADG.dataProvider it would try to display in a tree view. By setting the HierarchicalCollectionView.showRoot flag to false to avoid displaying the single group parent.

By specifying the default ADGItemRenderer as ADG.groupItemRenderer we can prevent the group icons from getting displayed.

The source is here.

Please note I have not tested this example extensively. So some bug fixes may be required before it can be used in production.


Nathalie said...

I cannot get the "showRoot" set to false to actually stop displaying the invalid root element. Running your example source out of the box simply gives me a grid with "Not Available" in the first column and no other data. If I remove the item Renderer then I can select the "Not Available" folder and see the sub grouped data. But otherwise, I cannot get this example to work.

What version of flex are you using? I am using build 3.0.183654. I'm wondering what else might make this example not work for me. It's such a useful idea.

Thanks for your help in advance.

Sreenivas said...

I am using 3.0.185310 build. It works fine on some other machines I tested with the same build. I think this is Flex 3 M3 Beta 2 version.

Sreenivas said...

I think I have a later build than Flex 3 Beta 2. You may need to wait for next public beta. Sorry.

core000 said...

s, can this be done via hierarchical strucuture?

Sreenivas said...

Summary for hierarchical data is not possible out of the box because we require a collection which can keep the summary rows separate from the original collection.

Surender said...
This comment has been removed by the author.
Surender said...

Can this be done while we group it on some known field ,where the summary placlement is at group level.Means each group will have their own summary placed at group level and one last row will contain the total summary of each group?

Sreenivas said...

I am unable to exactly visualize what you want but I guess it should be possible.

Surender said...

I mean iam using a grouping collection in advance data grid where grouping is happening on somefield and summary being placed at group level.Suppose there are 5 groups formed each showing their summary at group level.
Now i want an extra row to be added at the end exactly the way u have done showing total of individiual group summaries.
suppose there are 5 groups.
alex 5000
rahul 10000
sachin 12000
hemant 5000
nishant 5000
total 37000
top 5 being group node and total being a single entry at the end.

Sagar Bora said...

First of all. thanks for the post. It has helped a lot.

I would like to point to one discrepancy here. For the data already present in the 'flatData', sorting works fine, i.e. if you sort by 'Territory_Rep', TOTAL either goes to the top of the grid or the bottom. But, if you add a record to 'flatData' such as

{ Region:"ABC", Territory:"XYZ" , Territory_Rep:"Yahoo Microsoft" , Estimate:45000 , Actual:52888 }then, if you sort, the TOTAL field goes in between, which looks kind of different.
Can you please guide me as to how should I prevent the SUMMARY ROW being sorted.

Thanks again!

Sreenivas said...

Currently GC doesn't support sorting after grouping has been done. If you want the functionality badly please file a bug report here

Priya said...

Is it possible to add a SummaryRow
in the plain DataGrid using GroupingCollection?