Skip to content

Instantly share code, notes, and snippets.

@stlsmiths
Created August 31, 2012 17:03
Show Gist options
  • Save stlsmiths/3555897 to your computer and use it in GitHub Desktop.
Save stlsmiths/3555897 to your computer and use it in GitHub Desktop.
DT formatters
//
// trying to implement "named" formatters ... as {key:'foo', formatter:"currency2", ...}
//
// getting close ... http://blunderalong.com/yui/dtb/dt_formatters1.html
//
YUI.add("dt-formatters", function(Y){
var DtFormatStrings = {
currency : { prefix:'$', decimalPlaces:2, thousandsSeparator:',' },
currency2 : { prefix:'$', decimalPlaces:2, thousandsSeparator:',' },
comma2 : { decimalPlaces:2, thousandsSeparator:','},
shortDate : { format:'%D'},
isoDate : { format:'%FT%T'}
};
function fmtCaller(o){
var fmtType = o.column.ftype,
fmtStr = o.column.fstring || null,
fmtObj = ( fmtStr ) ? DtFormatStrings[fmtStr] : {};
if (fmtType === "date")
o.value = Y.DataType.Date.format(o.value,fmtObj);
if (fmtType === "number")
o.value = Y.DataType.Number.format(o.value,fmtObj);
}
var MyFormatters = {
isoDate :function(o){
o.column = Y.merge(o.column,{ftype:'date',fstring:'isoDate'});
fmtCaller.call(this,o);
},
currency2 : function(o){
o.column = Y.merge(o.column,{ftype:'number', fstring:'currency2'});
fmtCaller.call(this,o);
}
// ... more to come!
};
// How about overriding Y.DataTable.BodyView._createRowHTML to allow using "named" formatters ?
// somewhere around ...
// http://yuilibrary.com/yui/docs/api/files/datatable_js_body.js.html#l685
Y.DataTable.Formatters = MyFormatters;
}, "0.1");
YUI().use( "datatable", "datatype", "cssfonts", "cssbutton", "dataschema-json",
"dt-formatters", function (Y) {
var cols = [
{ key:"estart_date", label:'Start Date', formatter: Y.DataTable.Formatters['isoDate'], sortable:true },
{ key:"esalary", label:'Salary', formatter: Y.DataTable.Formatters['currency2'], sortable:true }
];
var dt = new Y.DataTable({
columns: cols,
data: ...
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment