Solved

# Need a Calculate Table Column Total Function

Posted on 2005-05-12
772 Views
I have some ASP pages where I need to figure out how to calculate a total of all rows I am writing out.  I am writing table rows and columns.  What I did was for each column in ASP I added a number to the TD tag such as

<TR>
<TD name=COL1TOTAL1>
</TD>
<TD name=COL2TOTAL1>
</TD>
<TD name=COL3TOTAL1>
</TD>
</TR>

<TR>
<TD name=COL1TOTAL2>
</TD>
<TD name=COL2TOTAL2>
</TD>
<TD name=COL3TOTAL2>
</TD>
</TR>

I did not know if I should use textboxes instead but I am only writing out values.   I am thinking I can probably just do this within the ASP Code as well, but I want to know if there is a Javascript Total Function where I can do something like this and for each Column do some loop and a string parse to loop and grab each Column name and total that column.  Then I am looking for something to handle a row as well like ROW1TOTAL, ROW2TOTAL.  Anyone have anything I can play with.  Thanks
0
Question by:sbornstein2

LVL 36

Expert Comment

Hi sbornstein2,

I'm not exactly sure what you're asking. Calculate the totals of what columns in what table? Can you elaborate please?

Regards,
Zyloch
0

Author Comment

I have a table and within that table I have ASP code that will write out a new row dynamically for each record.  Each record will have 4 columns, Order1 Amt, Order2 Amt, Order3 Amt, Total Order.  Then when the row gets written I am just adding a number to each column name, I thought this might allow me to loop through and calc totals on each column, if not then just ignore it.  So it looks like this

<TABLE>
<TR>
<TD name="Order1Amt-1">50.00
</TD>
<TD name="Order2Amt-1">60.00
</TD>
<TD name="Order3Amt-1">70.00
</TD>
<TD name="OrderTotal-1">180.00
</TD>
</TR>

<TR>
<TD name="Order1Amt-2">20.00
</TD>
<TD name="Order2Amt-2">30.00
</TD>
<TD name="Order3Amt-2">20.00
</TD>
<TD name="OrderTotal-2">70.00
</TD>
</TR>

<TR>
<TD name="Order1Amt-3">10.00
</TD>
<TD name="Order2Amt-3">15.00
</TD>
<TD name="Order3Amt-3">8.00
</TD>
<TD name="OrderTotal-3">33.00
</TD>
</TR>
--The loop will end once the records all write out.  Now I need a total row to total each column like this.
<TR>
<TD name="TOTALORDER1">80.00
</TD>
<TD name="TOTALORDER2">105.00
</TD>
<TD name="TOTALORDER3">98.00
</TD>
<TD name="TOTALORDERS">283.00
</TD>
</TR>
</TABLE>

That last row is what I am looking to calc with Javascript and also potentialy later the Row Totals across if possible.

0

LVL 36

Accepted Solution

I see. You would want to give your table an id, something like "orderTable" (<table id="orderTable">)

Then, you can try something like this: (using the DOM -- hopefully it works in most modern browsers -- I can't really test in school)

//Pass the function your table object, called with document.getElementById or something of the sort and column number
//Have your total row as the last row of the table
function collectRows(tbl,colnum) {
var total = 0;
var tblRows = tbl.getElementsByTagName("tr");
for (var i=0;i<tblRows.length-1;i++) {
tblCol = tblRows.item(colnum).data;
total += parseFloat(tblCol);
}
return(total);
}
0

Author Comment

Thanks. :)
0

## Featured Post

### Suggested Solutions

Both jQuery and Mootools offer some nice, easy to use scripts. Mootools is best known for their interactive slideshows and jQuery for their menus, but both have many other uses as well.   jQuery is basically a toolkit with many DOM (http://en.wik…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…