Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Need a Calculate Table Column Total Function

Posted on 2005-05-12
4
Medium Priority
?
786 Views
Last Modified: 2012-08-14
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
Comment
Question by:sbornstein2
  • 2
  • 2
4 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 13993271
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

by:sbornstein2
ID: 13995157
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

by:
Zyloch earned 200 total points
ID: 13997837
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

by:sbornstein2
ID: 13997894
Thanks. :)
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question