Sum Table Column using JQuery

by daz 11/12/2009 7:27:00 PM

Jquery library helps a lot. It simplify and decrease coding time for all develppers

Here is how you can sum column of a Html table that uses tbody, thead, tr and td or anything

The first and all you need an ID of your tag. either a table or thead...td

<table>

My headers goes here

thead  > <tr><th >&nbsp;</th></thead>

 the header is Outside of my tData . Thus i can sum all the data in colum. Else if you have the header WithHeader = true when u call the function in (**) 

<script type="text/javascript">  

pretty easy.. If U have problem figuring it out should me an email.

$(document).ready(

function() {

$(

"#ProspectIn").html(JquerySum("firstData", 3, false));

$(

"#ProspectOut").html(JquerySum("firstData", 5, false));

$(

"#Prospect").html(JquerySum("firstData", 7, false));

$(

"#Client").html(JquerySum("firstData", 8, false));

});

 

</script>

(**) 

 

function JquerySum(tagId, columnIndex, WithHeader ) {

 

var sum= 0;

$(

"#" + tagId+ " tr" + (WithHeader ? ":gt(0)" : ""))

.children(

function() {

sum+= parseInt($(

this).html());

});

 

return sum;

}

"td:nth-child(" + columnIndex + ")")

.each(

"td:nth-child(" + columnIndex + ")")

.each(

function() {

sum+= parseInt($(

this).html());

});

 

return sum;

}

 

 

Tags:

About the author

Name of author
DavidZ
.Net Programmer
E-mail me Send mail

Calendar

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

Recent comments

Authors

Categories


Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Sign in