calculated DG columns
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.
|