<style scoped>
/* custom styles for this example */
.dt-example {margin:1em;}
/* css to counter global site css */
.dt-example th {text-transform:none;}
.dt-example table {width:auto;}
.dt-example caption {display:table-caption;}
.notice {
background: #faf3d1;
border: 1px solid #eac9a9;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: px;
padding: 0 1em;
-moz-box-shadow: 0 0 5px #ccc8b3;
-webkit-box-shadow: 0 0 5px #ccc8b3;
box-shadow: 0 0 5px #ccc8b3;
margin-bottom: 1em;
}
</style>
<div class="notice">
<p>
<strong>NOTICE</strong>: This example is for the <strong>deprecated
version of DataTable</strong>. The components referred to here will be
removed in future versions of YUI</strong>. If you are unable to
upgrade to <a href="../datatable/index.html">the latest DataTable
version</a> due to unresolvable feature conflicts from version 3.4.1 or
prior, please <a href="../../../projects/yui3/newticket/">file a
ticket</a>.
</p>
</div>
<div class="intro">
<p>Custom format row data for display with string templates or custom functions.</p>
</div>
<div class="example yui3-skin-sam">
{{>datatable-formatting-source}}
</div>
<h2>Formatting Row Data for Display</h2>
<p>Data can be stored in one format but be displayed in a different format. For instance, prices can be stored as numbers but be displayed as "$2.99", and birthdays can be stored as date objects but be displayed as "12/9/2009".
<p>Simple formatting can be defined with a string template on the column definition.</p>
```
YUI().use("datatable-base-deprecated", function(Y) {
var cols = ["id","name", {key:"price", formatter:"\${value}"}],
data = [
{id:"ga-3475", name:"gadget", price:6.99},
{id:"sp-9980", name:"sprocket", price:3.75},
{id:"wi-0650", name:"widget", price:4.25}
],
table = new Y.DataTable.Base({
columnset: cols,
recordset: data,
caption: "Data formatting with string template"
}).render("#template");
```
<p>When a calculation is needed, define a custom function that generates markup for the data cell. The custom formatter function receives an object with the following properties: `{tbody, tr, classnames, headers, rowindex, record, column, data, value}`.</p>
```
// The custom formatter function recieves an object with the properties:
// {tbody, tr, td, classnames, headers, rowindex, record, column, data, value}
var calculate = function (o){
var record = o.record;
return "$"+(record.getValue("price") - record.getValue("cost"));
},
cols = [
"id",
"name",
{ key: "profit", formatter: calculate }
],
data = [
{id:"ga-3475", name:"gadget", price:6.99, cost:4.99},
{id:"sp-9980", name:"sprocket", price:3.75, cost:2.75},
{id:"wi-0650", name:"widget", price:4.25, cost:3.25}
],
dt = new Y.DataTable.Base({
columnset: cols,
recordset: data,
caption: "Data formatting with custom function"
}).render("#function");
```
<p>The DataType utility can be used to help format date objects. This example
also uses the `emptyCellValue` column configuration to supply a custom cell
value in the case of missing data.</p>
```
YUI().use("datatype-date", "datatable-base-deprecated", function (Y) {
// The custom formatter function recieves an object with the properties:
// {tbody, tr, classnames, headers, rowindex, record, column, data, value}
var formatDates = function (o){
return o.value &&
Y.DataType.Date.format(o.value, { format: "%m/%d/%Y" });
},
cols = [
"id",
"name",
{ key: "date", formatter: formatDates, emptyCellValue: "(unknown)" }
],
data = [
{id:"ga-3475", name:"gadget", date:new Date(2006, 5, 1)},
{id:"sp-9980", name:"sprocket", date:new Date(2004, 8, 16)},
{id:"wi-0650", name:"widget"} // no date for this record
],
dt = new Y.DataTable.Base({
columnset: cols,
recordset: data,
caption: "Data formatting with DataType.Date"
}).render("#dates");
```