DataTable Widget
:: Custom Formatting
Sample Code
CSS:
/* custom css*/ #formatting {margin:1em;} #formatting table {border-collapse:collapse;} #formatting th, #formatting td {border:1px solid #000;padding:.25em;} #formatting th {background-color:#696969;color:#fff;}/*dark gray*/ #formatting .yui-dt-odd {background-color:#eee;} /*light gray*/
Markup:
JavaScript:
// Define a custom formatter for one of the Columns var myFormatter = function(elCell, oRecord, oColumn, oData) { if(oRecord["Unread"]) { elCell.innerHTML = "X"; } }; var myColumnHeaders = [ {text:"New",formatter:myFormatter}, {key:"Date",type:"date"}, {key:"To"}, {key:"Subject"} ]; var myColumnSet = new YAHOO.widget.ColumnSet(myColumnHeaders); var myDataSource = new YAHOO.util.DataSource(YAHOO.example.Data.emails); myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON; myDataSource.responseSchema = { resultsList: ["messages"], fields: ["Date","To","From","Subject","XID","Date","Unread"] }; // Override the built-in Column formatter YAHOO.widget.Column.formatDate = function(elCell, oRecord, oColumn, oData) { var oDate = oData; var sMonth; switch(oDate.getMonth()) { case 1: sMonth = "Jan"; break; case 2: sMonth = "Feb"; break; case 3: sMonth = "Mar"; break; case 4: sMonth = "Apr"; break; case 5: sMonth = "May"; break; case 6: sMonth = "Jun"; break; case 7: sMonth = "Jul"; break; case 8: sMonth = "Aug"; break; case 9: sMonth = "Sep"; break; case 10: sMonth = "Oct"; break; case 11: sMonth = "Nov"; break; case 12: sMonth = "Dec"; break; } elCell.innerHTML = sMonth + " " + oDate.getDate() + ", " + oDate.getFullYear(); }; var myDataTable = new YAHOO.widget.DataTable("formatting",myColumnSet,myDataSource,{caption:"Example: Custom Formatting",rowSingleSelect:true});