0 Replies Latest reply on Jul 13, 2006 4:42 AM by cariboucactus

    datagrid column calculate totals

    cariboucactus
      Hi - hope someone can help with this datagrid issue - I think it's just a small bit of code I've got to tweak. Might be some useful information to someone here anyway.

      I've got a datagrid populated by info from mysql via amfphp.

      I've managed to create a column that multiplies the qty and price fields:

      root.dgCart.addColumn(column2);

      var column2 = new DataGridColumn();
      column2.headerText = "sum";

      column2.labelFunction = customLabelFunction2;
      column2.columnName = 'sum';

      function customLabelFunction2(item:Object):Number {
      // if (item == undefined) return undefined;
      var sumtotal = round2D(Number(item.item_price) * Number(item.qty));
      return sumtotal;
      }


      //helps to make decimals
      function round2D (nr){
      var tmp = String(nr)
      var pos = tmp.substr(tmp.indexOf(".")+1)
      if(tmp.indexOf(".")==-1) return nr+".00" //no decimals
      else if(pos.length==1) return nr+"0" //1 decimal
      else return Math.round (Number(nr) * 100) / 100 //2 or more
      }


      Now I'd like to get a total of all the items in the 'sum' column - the following code works to add up the 'price' column but the same approach doesn't seem to work for the 'sum' column - perhaps because the figures are generated by a function?

      function getTotal(){
      var totalPrice = 0;
      for(var i=0; i<root.dgCart.length; i++) totalPrice += Number(root.dgCart.getItemAt(i).item_price)
      root.txttotal.htmlText = "<b>Total:</b> "+ round2D(totalPrice);
      }


      If anyone here could shed the light, I'd be sooooo greatful!