Created
December 1, 2009 18:26
-
-
Save xeb/246510 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
* jQuery.math v1.0 | |
* http://epicapp.com/jquery.math | |
* | |
* Copyright (c) 2009 Mark Kockerbeck | |
* Dual licensed under the MIT and GPL licenses. | |
*/ | |
(function($){ | |
$.math = { version: 1.0 } | |
$.extend($.math, { | |
settings: { | |
decimals: 2, | |
extractNumbers: true, | |
addExtractedNumbers: false | |
} | |
}); | |
$.subtotal = { | |
Display: { OnEachRow: 0, OnLastRow: 1 } | |
} | |
$.fn.getValue = $.getValue = function(v) { | |
if(v == undefined) v = this; | |
return $(v).text() == '' ? $(v).val() : $(v).text(); | |
} | |
$.fn.getNumber = $.getNumber = getNumber = function(e) { | |
var settings = $.math.settings; | |
if(e == undefined) e = $(this).getValue(); | |
if(!settings.extractNumbers && isNaN(e)) return 0; | |
if(!isNaN(e)) return e - 0; | |
var matches = e.match(/[\d\.]+/g), sum; | |
if(settings.addExtractedNumbers) sum = 0; else sum = ''; | |
for(var i in matches) { | |
if($.math.settings.addExtractedNumbers) { | |
sum += matches[i] - 0; | |
} else { | |
sum = sum + '' + matches[i]; | |
} | |
} | |
return sum - 0; | |
} | |
$.fn.round = $.round = function(number, length) { | |
if(length == undefined) { | |
length = number; | |
number = $.getNumber($.getValue(this)); | |
} | |
return Math.round(number * Math.pow(10, length)) / Math.pow(10, length) ; | |
} | |
$.fn.sum = $.sum = function(arg) { | |
var settings = $.math.settings, sum = 0; | |
if(arg == undefined) { | |
$(this).each(function(i,e){ | |
sum += $.getNumber($.getValue(e)); | |
}); | |
} else { | |
for(var i in arg) { | |
sum += $.getNumber(arg[i]); | |
} | |
} | |
return $.round(sum, settings.decimals); | |
} | |
$.fn.multiply = function(selector, factor) { | |
$(this).each(function(i,e){ | |
var value = $.getNumber($(e).parent().children(selector).text()); | |
value = $.round( (value * factor) , 2 ); | |
$(e).text( value ); | |
}); | |
return this; | |
} | |
subtotals = { } | |
$.fn.total = function(options) { | |
subtotals = { } // clear subtotals for chaining | |
$(this).each(function(i,e){ | |
var group = $(this).parent().children(options.groupBy).text(), value = $(this).getNumber(); | |
if(subtotals[group] == null) subtotals[group] = { sum: value, firstRow: $(this) }; | |
else subtotals[group].sum += $.round(value, $.math.settings.decimals); | |
subtotals[group].lastRow = $(this); | |
if(options.display == $.subtotal.Display.OnEachRow) { | |
$(this).parent().children(options.setTo).text($.round(subtotals[group].sum, $.math.settings.decimals)); | |
} | |
}); | |
if(options.display == $.subtotal.Display.OnLastRow && options.setTo != null) { | |
for(var i in subtotals) { | |
$(subtotals[i].lastRow).parent().children(options.setTo).text($.round(subtotals[i].sum, $.math.settings.decimals)); | |
} | |
} | |
return this; | |
} | |
$.fn.subtotal = function(options) { | |
return $(this).total($.extend({ groupBy: '.groupBy', setTo: '.subtotal', display: $.subtotal.Display.OnLastRow }, options)); | |
}; | |
$.fn.runningTotal = function(options) { | |
return $(this).total($.extend({ groupBy: '.groupBy', setTo: '.runningTotal', display: $.subtotal.Display.OnEachRow }, options)); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment