Thursday, December 24, 2009

Flex Datagrid sum column problem solution

I looping through the datagrid by column with plus row by row amount , found that flex cannot simply looping and sum it. You must using the myAmount.toFixed(2) method to sum up.

eg. myDataGrid

Debit       Credit
104.54       0.00
100.00       0.00
  60.00       0.00

Before :

i using FOR loop through the datagrid like below:
----------------------------------------------------------------
var tempDebitAmount : Number = 0;
for(var i:int ; i < myDataGrid.dataProvider.length ; i++){
     tempDebitAmount += Number(myDataGrid.dataProvider[i].@debit);
}

trace(tempDebitAmount );
----------------------------------------------------------------
result is : 264.53999999999994
I really not understand why it become like this?
Solution :
-----------------------------------------------------------------
var tempDebitAmount : Number = 0;
for(var i:int ; i < myDataGrid.dataProvider.length ; i++){
      tempDebitAmount = Number(tempDebitAmount.toFixed(2)) + Number(myDataGrid.dataProvider[i].@debit);
}
trace(tempDebitAmount );

----------------------------------------------------------------

result is : 264.54

1 comment:

  1. Esto me funciono:
    public function suma():void{
    //var tempDebitAmount : Number = 0
    //Alert.show(dtg2.dataProvider.length.toString());

    var total : Number = 0;
    var total2 : Number = 0;
    for(var i:int ; i < dtg2.dataProvider.length ; i++){
    // Alert.show("Gol"+i);
    total = total +Number(dtg2.dataProvider[i].Tienda);
    total2 = total2 + Number(dtg2.dataProvider[i].Distribuidor);
    //tempDebitAmount = Number(tempDebitAmount.toFixed(2)) + Number(dtg2.dataProvider[i].@tienda);
    // Alert.show("Gol2"+i);
    }
    trace(total);
    txt_tienda.text=total.toString();
    txt_dis.text=total2.toString();

    ReplyDelete

Your comments is appreciate.