Thursday, November 29, 2007

Customizing grouping using GroupingField.groupingFunction in GroupingCollection

It is possible to generate custom groups using groupingFunction property of GroupingField. Take a look at the following sample. By choosing different options from "Choose age range" users can view the same data in different groups.

The sample uses fictitious data of web site visitors by age and location. The example uses COUNT summary option to show how many visitors exist in each group.

The only thing to remember while writing a groupingFunction is that the return value should not change the sorting order being used for the dataField. Another way to put it is, the groupingFunction should return group names in a sorted order only. It shouldnot return group names in a random fashion.

The source can be found here.

12 comments:

Unknown said...

Hi,

I am interested if this method can be reproduced with date ranges. I tried the following groupingFunction and it did not work so well.....

private function monthlyFunction(data:Object, field:GroupingField):String
{
var newDate:Date = data[field.name] as Date;
var dateLower:Date = new Date(newDate.getFullYear(), newDate.getMonth(), 1);
var dateHigher:Date = new Date(newDate.getFullYear(), newDate.getMonth() + 1, 1);

return Number(dateLower).toString()+"-"+Number(dateHigher).toString();
//return DataFormatters.MONTHS[newDate.getMonth()] + newDate.getFullYear().toString()+ "-" + DataFormatters.MONTHS[newDate.getMonth() + 1] + newDate.getFullYear().toString();
}

Any thoughts?

Sreenivas said...

I had posted a date grouping similar to Outlook here.

See if that helps.

Unknown said...

Thanks, It seems I had forgetten a compare function. It works now.

Flamingo said...

How to remove the default sort applied to the grouped column? I want to get rid of it and keep the original sorting of my XML.
somewhere i read "compareFunction" could do it....would you post an example for this function please?
thank you

Sreenivas said...

I don't know what you mean by keep the original sorting of XML data you have. Doest it mean it can be even unsorted ?

It is necessary to sort the data for grouping without which it is not possible to group reliably.

If you very much want to keep the original sorting order assuming you know what it was (other than unsorted) you have to re-apply the sorting to the ADG.dataProvider.

Flamingo said...

Thanks for your reply. Yes. It should be unsorted. I just have to follow the XML order. for that what should i do?

Sreenivas said...

Nothing can be done in that case as the unsorted order cannot be restored in any fashion.

Unknown said...

Hi Sreenivas,

Can u add an extra row at the end which will display total of counts
whithout removing grouping?

like

Age Count
Total 20

Unknown said...

Sreenivas, suppose I have a data source that contains two fields: name and food.

The data (sorted by name, food) looks like:
Adam Bigmac
Adam Hotdog
Helen Bigmac
Helen Candybar
Helen Hotdog

I want to group it by name and then, within each group, sort by food. I.e., I want to preserve the sort order.

But after grouping by name, the food loses its sort order. How can I manage to retain the sort order?

Unknown said...

Forgot to mention that I don't want to use 2 groupingfields because that would affect the appearance of the AdvancedDataGrid. I want only 1 groupingfield to be visible and sort by the other fields.

Sreenivas said...

The way to get sorting to work with grouping collection is to group on the field required and then apply sort to adg directly.

gc.refresh();
var view:HierarchicalCollectionView = adg.dataProvider as HierarchicalCollectionView;
var mySort:Sort = new Sort();
var fields:Array = [ new SortField("food") ];
mySort.fields = fields;
view.sort = mySort;
view.refresh();

Roberto said...

Hi, Sreenivas,
My new problem... My ADG is working, but I have a slider with a range of values away of ADG. When I set these values I want the Summary row in ADG is filter, only showing totals between this range. Can you help me, please?

Thank you again

Roberto