Need a Calculate Table Column Total Function

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
sbornstein2Asked:
Who is Participating?
 
ZylochCommented:
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
 
ZylochCommented:
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
 
sbornstein2Author Commented:
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
 
sbornstein2Author Commented:
Thanks. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.