Skip to content

Instantly share code, notes, and snippets.

@stlsmiths
Created November 22, 2011 02:51
Show Gist options
  • Save stlsmiths/1384755 to your computer and use it in GitHub Desktop.
Save stlsmiths/1384755 to your computer and use it in GitHub Desktop.
// Define a custom formatter to format Numbers as US currency format, and if negative, to
// set color to RED and surround with parenthesis;
// Example: ( $ 12,345.67 ) in red
// Note: Function needs to be defined BEFORE the Column definitions that use it !!
var fmtNegCurrency = function(elCell, oRec, oCol, oData) {
var num_prefix='',
num_suffix='';
if ( oData < 0.0 ) {
YAHOO.util.Dom.setStyle(elCell,'color','red');
num_prefix = '(';
num_suffix = ')';
}
// define format configuration for Number.format function ...
var fmtCurrency = {
prefix : '$ ',
thousandsSeparator: ',',
decimalSeparator: '.',
decimalPlaces: 2
}
// "format" the number using YAHOO.util.Number.format ... (see API for details)
var num_fmt = YAHOO.util.Number.format( oData, fmtCurrency );
// Insert the formatted data in the Cell, put prefix / suffix on if defined .
elCell.innerHTML = num_prefix + num_fmt + num_suffix;
}
// Use the formatter in your DataTable column ...
var myColumns = [
// your Column names here ...
{ key:'totals', label:'Net Totals', formatter:fmtNegCurrency }
// other column names ....
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment