:: Forum >> Version 2 >>

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([numnumnum]);


//    Running Total Function    
function balance1(colrow

var 
cell2 obj.getCellValue(0row);
var 
cell1 obj.getCellValue(0row-1);    
var 
total cell1 cell2;  
return 
total;
}

//   Set Column Indices and Running Total Function into Column 1
obj.setColumnIndices([012]);
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 BalanceRunning 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(00)

return 
obj.getCellValue(0row) + obj.getCellValue(colrow 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(colrow) { 
    var 
count 0;
    var 
a=obj.getCellValue(0,row); //    Column for an incrementing row ID
    
var b=obj.getRowCount();
    var 
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