JQuery Total Calculated Column in SharPoint 2010

Working off Paul Grenier’s post JQuery for Everyone: Total Calculated Columns and some hair loss, I got this working in SharePoint 2010.

Need to sum / total a calculated column in SharePoint 2010, here is the answer.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script> 
<script type="text/javascript">
function addCommas(nStr) {//formats number
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
$(function() {//sums money in specific list column
	var col = 2; //which column to sum
	var m = "$"; //change to "" for non-money format
	var headers = $("table.ms-listviewtable:first> tbody> tr:first th").get();
 	var arrayList = $("table.ms-listviewtable:first> tbody> tr:gt(1)").find(">td:eq("+col+")").get();
	var x = 0;
	var p1 = "";
	var p2 = "";
	$.each(arrayList, function(){
		x += Number($(this).text().replace(/\$|,|\)/g, "").replace(/\(/g,"-"));
	});
	if (x < 0) {//format for negative numbers
		p1 = "(";
		p2 = ")";
		x = Math.abs(x);
	}
	
	$(".ms-listviewtable:first> tbody> tr:eq(1)").find(">td:eq("+col+")")
		.css("text-align","middle")
		.html("<b>Total = "+p1+m+addCommas(x.toFixed(2))+p2+"</b>");
});
</script>

3 thoughts on “JQuery Total Calculated Column in SharPoint 2010

  1. This is great! Thanks for the posting. Do you have an updated version to work with groupings in the view? If I add groupings, my average calculation goes to 0. (I modified it to do an average instead of total)

Leave a Reply

Your email address will not be published. Required fields are marked *