Created
November 22, 2011 02:51
-
-
Save stlsmiths/1384755 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// 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