:: Forum >>

Row By Row Running Total

Hi,

I am trying to get a row by row running total and would like advice on how..

I have tried to following,

// Set Grid Headers, Cells and Formats
var obj = new AW.UI.Grid;
var myHeaders = ["Invoice Balance","Running Total"];
var myCells = [["10.00"],["20.00"],["30.00"],["40.00"]];
var num = new AW.Formats.Number;
num.setTextFormat('#,###.##');
obj.setCellFormat([num, num, num]);


// Running Total Function
function balance1(col, row)
{
var cell2 = obj.getCellValue(0, row);
var cell1 = obj.getCellValue(0, row-1);
var total = cell1 + cell2;
return total;
}

// Set Column Indices and Running Total Function into Column 1
obj.setColumnIndices([0, 1, 2]);
obj.setCellData(balance1,1);

// Set grid text
obj.setHeaderText(myHeaders);
obj.setCellText(myCells);

// set number of columns/rows
obj.setRowCount(myCells.length);

// write grid to the page
document.write(obj);


Which returns the following,

Invoice Balance, Running Total,
10.00 10.00
20.00 30.00
30.00 50.00
40.00 70.00




Please Help,

Jez
Jez
Saturday, September 12, 2009
You algorithm is slightly off as you're only adding the current invoice balance with its previous one. You should be adding the current invoice balance to the previous running total.

Here's an algorithm you can use to get the right figures -

if (row == 0)
return obj.getCellValue(0, 0)

return obj.getCellValue(0, row) + obj.getCellValue(col, row - 1)
Anthony
Saturday, September 12, 2009
Thanks That worked a treat!!

I did get there sort off with this, but I had to add a column for an incrementing row ID,

function runnintTotal(col, row) {
    var count = 0;
    var a=obj.getCellValue(0,row); // Column for an incrementing row ID
    var b=obj.getRowCount();
    var c = a-b;
    for(var i=0;i<obj.getRowCount()+c;i++)
    count += obj.getCellValue(1,i);
    return count;
    };


I have used your method in my script and it worked first time and is a much more methodical approach.

Thanks Again,

Jez
Jez
Saturday, September 12, 2009

This topic is archived.


Back to support forum

Forum search