Hi guys,
I try to get summary for each collumn in advanceddatagrid which is generated 
and grouped in runtime.
I try every sample I could find in web but didn't work. 
in my code some parts are remarked due different samples. 
I will appreciate if yo help me.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"; layout="vertical">
<mx:Script>
<![CDATA[
        import mx.collections.IViewCursor;
        import mx.collections.SummaryObject;
        import mx.controls.Alert;
        import mx.collections.SummaryRow;
        import mx.collections.SummaryField;
        import mx.collections.IHierarchicalCollectionView;
        import mx.controls.advancedDataGridClasses.AdvancedDataGridColumnGroup;
        import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
        import mx.utils.StringUtil;
        import mx.controls.dataGridClasses.DataGridColumn;
        import mx.collections.XMLListCollection;
        import mx.collections.ArrayCollection;

        [Bindable] private var dados:XML = 
        <employees>
        <employee>
        <name>Christina Coenraets</name>
        <phone>555-219-2270</phone>
        <email>[email protected]</email>
        <active>true</active>
        </employee>
        <employee>
        <name>Louis Freligh</name>
        <phone>555-219-2100</phone>
        <email>[email protected]</email>
        <active>true</active>
        </employee>
        <employee>
        <name>Ronnie Hodgman</name>
        <phone>555-219-2030</phone>
        <email>[email protected]</email>
        <active>false</active>
        </employee>
        <employee>
        <name>Joanne Wall</name>
        <phone>555-219-2012</phone>
        <email>[email protected]</email>
        <active>true</active>
        </employee>
        <employee>
        <name>Maurice Smith</name>
        <phone>555-219-2012</phone>
        <email>[email protected]</email>
        <active>false</active>
        </employee>
        <employee>
        <name>Mary Jones</name>
        <phone>555-219-2000</phone>
        <email>[email protected]</email>
        <active>true</active>
        </employee>
        
        </employees>

        private var dados2:XML = 
        <employees>
        <employee name="Christina Coenraets" 
                phone="555-219-2270"
                email="[email protected]"
                active="true" />
        <employee name="Louis Freligh" 
                phone="555-219-2100" 
                email="[email protected]"
                active="true" />
        </employees>
                var columns:Array = new Array()
        private function generateCols(input:XMLList, useAttributes:Boolean = 
false):void
        {
                
                
                var e1:XML = input[0];

                var children:XMLList ;
                if (useAttributes)
                {
                        children = e1.attributes();
                }
                else
                {
                        children = e1.children();
                }
        
                for each(var child:XML in children)
                {
                        var col:AdvancedDataGridColumn = new 
AdvancedDataGridColumn();
                        col.dataField = useAttributes ? "@" + child.name() : 
child.name();
                        var fieldName:String = child.name();
                        col.headerText = fieldName.charAt(0).toUpperCase() + 
fieldName.substr(1);
                        columns.push(col);


                        
                        
                }

                gc.refresh();
                dg1.dataProvider = gc;
                dg1.columns =columns

                addSumm()
                
        }
                private var count:int = 0;
                
                private function grpObjFunc(value:String):Object
                {
                        // we need to assign the same uid for same grouped 
Objects
                        // use count or value + count
                        return {uid:value + count++};
                }
                        var summary:SummaryRow
                 function addSumm():void
                {
                        
                                /*summary= new SummaryRow()
                                summary.summaryPlacement="group"
                                summary.fields= new Array()
                                var sf:SummaryField
                                for each (var col:AdvancedDataGridColumn in 
columns){
                                        Alert.show(col.dataField.toString())
                                        sf= new SummaryField()
                                        sf.dataField=col.dataField
                                        sf.label=col.dataField
                                        sf.operation="COUNT"
                                        summary.fields.push(sf)
                                        summary.summaryObjectFunction=objFunc
                        }
                        
                gc.summaries=summary.fields*/
                        // add summaries and re-group
                                                        
//IHierarchicalCollectionView(dg1.dataProvider).openNodes = openNodes;
                                var sr:SummaryRow = new SummaryRow();
                                sr.summaryObjectFunction = objFunc;
                                var sf:SummaryField = new SummaryField("phone");
                                sf.summaryFunction = func;
                                sr.fields = [sf];
                                sr.summaryPlacement = "last";
                                gf.summaries=[sr]

                        if (dg1.dataProvider is IHierarchicalCollectionView)
                        {

                                var openNodes:Object = 
IHierarchicalCollectionView(dg1.dataProvider).openNodes;
                                count = 0; // reset the counter
                                
                                gc.refresh();
                                
                                dg1.dataProvider = dg1.columns;
                                dg1.validateNow();

                        }
                }
                
private function objFunc():SummaryObject
{
var obj:SummaryObject = new SummaryObject();
obj.summary = true;
return obj;
}

private function func(itr:IViewCursor,field:String, str:String=null):Object
{
var sum:Number=0
while(!itr.afterLast)
{
trace(itr.current)
var value:Number = Number(itr.current.Licenses);
if(!isNaN(value))
{
sum+= value
}
itr.moveNext()
}
return sum
}

]]>
</mx:Script>

<mx:AdvancedDataGrid id="dg1" dataProvider="{dados.employee}"  
creationComplete="generateCols(dados.employee)">
</mx:AdvancedDataGrid>
        <mx:AdvancedDataGrid id="dg0" dataProvider="{gf.summaries}">
        </mx:AdvancedDataGrid>

<mx:GroupingCollection id="gc" source="{dados.employee}">
        <mx:Grouping groupingObjectFunction="grpObjFunc" >
                <mx:fields>
                        <mx:GroupingField id="gf" name="active"/>
                </mx:fields>
        </mx:Grouping>
</mx:GroupingCollection>

</mx:Application>



Reply via email to