1 Reply Latest reply on Jul 16, 2010 10:53 AM by nmkFlex4u

    How to access Summary data calculated in an Advanced Data Grid in another component ?

    nmkFlex4u

      Hi

       

      I am new to Flex and am currently working on a test project to populate an Array (dp) using data bindings. However, I have two requi
      rements.
      1. I need to create a column whose values are populated based on two columns in the dataprovider-- ArrayCollection. In this case, th
      e values in column datafield="calValue" are calculated at run-time using an inline Item renderer.
      2. I need to calculate the Average value of ALL the values populated in the custom column datafield="calValue".

       

      There are a couple of problems I am facing in this regard. Firstly, though I am able to create the custom column using an Item rende
      rer, I am not able to obtain the Average value of the column using the GROUPING COLLECTION2, GROUPING FIELD, SUMMARY ROW classes. Se
      condly, How do I access the summary value created in the initDG() function. I want to be able to pass the summary value calculated a
      nd displayed in the Advanced Data Grid to the property of another component.

       

      Example:
      <s:Label id="LabelSummary" />

       

      private function setLabelText():viod
      {
      LabelSummary.text = <SUMMARYVALUE>;
      }

       

      I need to be able to display the summary value in the Label component. Also, I have noticed that the summary row is a collection of
      Array objects. (I think--from my understanding !! not too sure though) I need the final summary value calculated in the initDG() fun
      ction to be accessible as an INTEGER or NUMBER. When I try typecasting and displaying the Label.Text property, I get the value:
      [object Summary Row] displayed in the Label. Hope I have given a good overview of the problem I am facing.

       

      Hope this provides a greater insight.

       

      My .mxml files are:

       

      SummaryWithActionScriptADG.mxml

       

      <?xml version="1.0" encoding="utf-8"?>

      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

         xmlns:s="library://ns.adobe.com/flex/spark"

         xmlns:mx="library://ns.adobe.com/flex/mx" >

       

      <s:layout>

      <s:VerticalLayout paddingTop="10"

        horizontalAlign="center"/>

      </s:layout>

       

          <fx:Script>

              <![CDATA[

      import mx.collections.ArrayCollection;

      import mx.collections.Grouping;

      import mx.collections.GroupingCollection2;

      import mx.collections.GroupingField;

      import mx.collections.SummaryField2;

      import mx.collections.SummaryRow;

       

      private var arrayData:Array=[

      {name:"banana",cat:"bakery",cost:.99,qty:2,amount:4},

      {name:"bread",cat:"bakery",cost:1.99,qty:3,amount:5},

      {name:"cheddar cheese",cat:"bakery",cost:4.52,qty:6,amount:4},

      {name:"sour cream",cat:"bakery",cost:.33,qty:2,amount:6},

      {name:"orange",cat:"bakery",cost:.52,qty:4,amount:7},

      {name:"donut",cat:"bakery",cost:.33,qty:2,amount:8},

      {name:"yogurt",cat:"bakery",cost:.99,qty:5,amount:9},

      {name:"milk",cat:"bakery",cost:2.99,qty:2,amount:10},

      {name:"apple",cat:"bakery",cost:1.05,qty:4,amount:8},

      {name:"colby cheese",cat:"bakery",cost:3.05,qty:4,amount:9}];

       

                  [Bindable]

                  private var dp:ArrayCollection=new ArrayCollection(arrayData);

       

      private function initDG():void

      {

      var myGroupColl:GroupingCollection2=new GroupingCollection2();

      myGroupColl.source=myADG.dataProvider;

      var group:Grouping=new Grouping();

      var gf:GroupingField=new GroupingField("cat");

      group.fields=[gf];

      myGroupColl.grouping=group;

       

      var sr:SummaryRow=new SummaryRow();

      var sf:SummaryField2=new SummaryField2();

      sf.dataField="qty";

      sf.summaryOperation="SUM";

      sf.label="summary";

      sr.fields=[sf];

      sr.summaryPlacement="group";

      gf.summaries=[sr];

      labelDisplay.text = gf.summaries.toString();

      myGroupColl.refresh();

      myADG.dataProvider=myGroupColl;

       

      var summaryVar:Array=new Array([sr]);

      var summaryVarInt:uint=uint(summaryVar[0]);

      labelDisplay.text=gf.summaries.pop();

      }

       

      private function summaryCal():void

      {

      var myGroupColl:GroupingCollection2=new GroupingCollection2();

      myGroupColl.source=myADG.dataProvider;

      var group:Grouping=new Grouping();

      var gf:GroupingField=new GroupingField("cat");

      group.fields=[gf];

      myGroupColl.grouping=group;

       

      var sr:SummaryRow=new SummaryRow();

      var sf:SummaryField2=new SummaryField2();

      sf.dataField="qty";

      sf.summaryOperation="SUM";

      sf.label="summary";

      sr.fields=[sf];

      sr.summaryPlacement="group";

      gf.summaries=[sr];

      labelDisplay.text = gf.summaries.toString();

      myGroupColl.refresh();

      myADG.dataProvider=myGroupColl;

       

      var summaryVar:Array=new Array([sr]);

      labelDisplay.text=summaryVar.length.toString();

      }

       

      private function testDG():void

      {

       

      }

              ]]>

          </fx:Script>

      <s:Label id="labelDisplay" text="Hello"/>

      <mx:AdvancedDataGrid id="myADG"

          dataProvider="{dp}"

      creationComplete="initDG()">

          <mx:columns>

              <mx:AdvancedDataGridColumn dataField="name"/>

              <mx:AdvancedDataGridColumn dataField="cost"/>

              <mx:AdvancedDataGridColumn dataField="qty"/>

      <mx:AdvancedDataGridColumn dataField="amount"/>

      <mx:AdvancedDataGridColumn  id="calCol" headerText="CalculatedValue" dataField="calValue">

      <mx:itemRenderer>

      <fx:Component> 

      <mx:Text text="{uint(data.qty)/(uint(data.amount)+uint(data.qty))}"/> 

      </fx:Component>

      </mx:itemRenderer>

      </mx:AdvancedDataGridColumn>

          </mx:columns>

      <mx:rendererProviders>

          <mx:AdvancedDataGridRendererProvider

              dataField="summary"

              columnIndex="1"

              columnSpan="2"

              renderer="renderers.SummaryTextForAS"/>

      </mx:rendererProviders>

      </mx:AdvancedDataGrid>

       

      </s:Application>

      SummaryTextforAS.mxml
      <?xml version="1.0" encoding="utf-8"?>
      <s:MXAdvancedDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx"
        focusEnabled="true">
      <s:Label id="lblData" top="0" left="0" right="0" bottom="0" text="Total number of items: {data.summary}" />
      </s:MXAdvancedDataGridItemRenderer>

       

      Thanks in advance.

       

      Regards
      -Krishna