4 Replies Latest reply on Nov 27, 2006 2:06 PM by ntsiii

    calculated DG columns

    ljonny18 Level 1
      Hi I am wondering if someone can help me….. I think what I am trying to do (and my actual problem) is quite simple, although I am the way going about it is possibly slightly different to the “norm” – so let me try to explain ;)

      I have a dataGrid (“myDG”) containing different columns, two of which are calculated columns that I want to change every time the other values within the dataGrid change

      Some example code:

      <mx:DataGrid editable="true" id="myDG " dataProvider="{myDGProvider}" change=”performCalcs()”>
      <mx:columns>
      <mx:DataGridColumn id=”colA” dataField="resultA" headerText="result A"/>
      <mx:DataGridColumn id=”colB” dataField="resultB" headerText="result B"/>
      <mx:DataGridColumn id=”colC” dataField="resultC" headerText="result C"/>
      <mx:DataGridColumn id=”colD” dataField="resultD" headerText="result D"/>
      </mx:columns>
      </mx:DataGrid>

      I also have a combo box that consists of two different statuses, and I want to perform different calculations to the dataGrid columns depending on the selected status.

      The 2 statuses are e.g. “calculation 1” and “calculation 2” and have the following conditions:

      If “calculation 1” is selected, then I want “colD” = colA + colB + colC.
      Else
      If “calculation 2” is selected, then I want “colD” = colA - colB - colC.
      Etc………

      <mx:ComboBox id="myCB" change="changeDiscountCode()" change=”performCalcs()”>
      <mx:dataProvider>
      <mx:Array>
      <mx:String>calculation 1</mx:String>
      <mx:String>calculation 2</mx:String>
      </mx:Array>
      </mx:dataProvider>
      </mx:ComboBox>

      The dataGrid is populated by an array acting as a dataProvider – the dataGrids dataProvider starts off empty and there is a button where the user can “add” a row to the dataGrid etc…:

      <mx:Button label="Add Row" click="addRow ()"/>

      public function addRow () {
      var newRow:Object = new Object();
      myDG_dg.addItem(newRow);
      }

      etc… with the object “newRow” assigning a default value to each column and successfully adding a new row to the dataGrid.

      As you can also see, I have added a “change” property to the dataGrid and comboBox to perform my calculations:

      public function performCalcs () {
      if(myCombo.text = “calculation 1”) {
      for(var x:int=0;x<DG_ myDGProvider.length;x++) {
      myDG[x].colD = myDG[x].colA + myDG[x].colB + myDG[x].colC;
      }
      }
      else if(myCombo.text = “calculation 2”) {
      for(var x:int=0;x<DG_ myDGProvider.length;x++) {
      myDG[x].colD = myDG[x].colA - myDG[x].colB - myDG[x].colC;
      }
      }

      }

      etc………….

      this all works fine….. but the calculations only take place / change when the actual columns are directly changed….
      I additionally want to automatically perform the DG column calculations when the comboBox is changed thus automatically changing and performing the DG calculations as the comboBox value is changed, without having to then change a value within the DG for the calculations to take place etc……

      Is there some way I can go about doing this??? Do I have to refresh the dataProvider somehow???


      Thanks,
      Jon.
        • 1. Re: calculated DG columns
          michael_ramirez44 Level 1
          Jon,

          You need to use the labelFunction property on "colD".

          colD.labelFunction = gridCal;

          private function gridCal(item:Object,column:DataGridColumn):String
          {
          if(myCombo.text = “calculation 1”)
          {
          return item["colA"] + item["colB"] + item["colC"];
          }
          else if(myCombo.text = “calculation 2”)
          {
          return item["colA"] - item["colB"] - item["colC"];
          }
          }
          • 2. Re: calculated DG columns
            ljonny18 Level 1
            hi, I used the labelFunction before but then changed it to the method I have shown above, as I want to have the results changed (the calculations to be completed) when the comboBox is also changed....

            the labelFunction only seems to perfrom the calculations when the DG itself is changed... and I want to be able to call the fuction also using the "change" property on the comboBox......

            i.e. if values have been entered and calculated using the 1st calculation ("calculation 1") and then I change the comboBox to "calculation 2" I thus want to re-calculate the values using the seconf calculation etc etc......

            thanks,
            Jon.
            • 3. Re: calculated DG columns
              ljonny18 Level 1
              hi, I used the labelFunction before but then changed it to the method I have shown above, as I want to have the results changed (the calculations to be completed) when the comboBox is also changed....

              the labelFunction only seems to perfrom the calculations when the DG itself is changed... and I want to be able to call the fuction also using the "change" property on the comboBox......

              i.e. if values have been entered and calculated using the 1st calculation ("calculation 1") and then I change the comboBox to "calculation 2" I thus want to re-calculate the values using the seconf calculation etc etc......

              thanks,
              Jon.
              • 4. Re: calculated DG columns
                ntsiii Level 3
                I think you are very close here.

                First, do you really mean "Array"? if so, that is one problem. Arrays do not emit the events necessary to tell the dataGrid to update its display. Instead, wrap the array in an ArrayCollection:
                var myDGProvider:ArrayCollection = new ArrayCollection(myArray)

                Second, use the ArrayCollection API, like addItem(), and setItemAt().

                This is the "gentlest" wat to do this, with the least visual updateing. Another option is to simply re-assign the array to the dataProvider:
                myDG.dataProvider = myDGProvider;

                Tracy