15153N/A/* custom styles for this example */
10017N/A/* css to counter global site css */
10017N/A <p>Custom format row data for display with string templates or custom functions.</p>
15153N/A<div class="example yui3-skin-sam">
15153N/A {{>datatable-formatting-source}}
10017N/A<h2>Formatting Row Data for Display</h2>
15153N/A<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".
15153N/A<p>Simple formatting can be defined with a string template on the column definition.</p>
15153N/AYUI().use("datatable", function(Y) {
15153N/A columns: [ "id", "name", { key: "price", formatter: "${value}" } ],
10017N/A { id: "ga-3475", name: "gadget", price: 6.99 },
10017N/A { id: "sp-9980", name: "sprocket", price: 3.75 },
10017N/A { id: "wi-0650", name: "widget", price: 4.25 }
10017N/A caption: "Data formatting with string template"
10017N/A When a calculation is needed, define a custom function that generates
10017N/A markup for the data cell. The custom formatter function receives an object
15153N/A B</a> in the DataTable user guide.
15153N/A// See the DataTable user guide for a list of properties on o.
15153N/A columns: [ "id", "name", { key: "profit", formatter: calculate } ],
15153N/A { id: "ga-3475", name: "gadget", price: 6.99, cost: 4.99 },
15153N/A { id: "sp-9980", name: "sprocket", price: 3.75, cost: 2.75 },
15153N/A { id: "wi-0650", name: "widget", price: 4.25, cost: 3.25 }
15153N/A caption: "Data formatting with custom function"
15153N/A<p>The DataType utility can be used to help format date objects. This example
15153N/Aalso uses the `emptyCellValue` column configuration to supply a custom cell
15153N/Avalue in the case of missing data.</p>
15153N/AYUI().use("datatype-date", "datatable", function (Y) {
15153N/A { key: "date", formatter: formatDates, emptyCellValue: "(unknown)" }
15153N/A { id: "ga-3475", name: "gadget", date: new Date(2006, 5, 1) },
15153N/A { id: "sp-9980", name: "sprocket", date: new Date(2004, 8, 16) },
10017N/A { id: "wi-0650", name: "widget"} // no date for this record