5
<title>Example: Formatting Row Data for Display</title>
6
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Maven+Pro:400,700">
7
<link rel="stylesheet" href="../../build/cssgrids/grids-min.css">
8
<link rel="stylesheet" href="../assets/css/main.css">
9
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
10
<script src="../../build/yui/yui-min.js"></script>
15
<h1>Example: Formatting Row Data for Display</h1>
20
<div class="yui3-u-3-4">
22
<div class="content"><style scoped>
23
/* custom styles for this example */
24
.example .yui3-datatable {
28
/* css to counter global site css */
33
display: table-caption;
43
<p>Custom format row data for display with string templates or custom functions.</p>
46
<div class="example yui3-skin-sam">
47
<div id="template" class="yui3-skin-sam dt-example"></div>
49
<div id="function" class="yui3-skin-sam dt-example"></div>
51
<div id="dates" class="yui3-skin-sam dt-example"></div>
53
<script type="text/javascript">
54
YUI().use("datatype-date", "datatable-base", function (Y) {
55
var colsTemplate = ["id","name", {key:"price", formatter:"${value}"}],
57
{id:"ga-3475", name:"gadget", price:6.99},
58
{id:"sp-9980", name:"sprocket", price:3.75},
59
{id:"wi-0650", name:"widget", price:4.25}
61
dtTemplate = new Y.DataTable({columns:colsTemplate, data:dataTemplate,
62
caption:"Data formatting with string template"}).render("#template"),
63
calculate = function(o){
64
return "$" + (o.data.price - o.data.cost).toFixed(2);
69
{ key: "profit", formatter: calculate }
72
{id:"ga-3475", name:"gadget", price:6.99, cost:4.99},
73
{id:"sp-9980", name:"sprocket", price:3.75, cost:2.75},
74
{id:"wi-0650", name:"widget", price:4.25, cost:3.25}
76
dtFunction = new Y.DataTable({columns:colsFunction, data:dataFunction,
77
caption:"Data formatting with custom function"}).render("#function"),
78
formatDates = function(o){
80
Y.DataType.Date.format(o.value, {format:"%m/%d/%Y"});
85
{ key: "date", formatter: formatDates, emptyCellValue: "(unknown)" }
88
{id:"ga-3475", name:"gadget", date:new Date(2006, 5, 1)},
89
{id:"sp-9980", name:"sprocket", date:new Date(2004, 8, 16)},
90
{id:"wi-0650", name:"widget"} // no date for this record
92
dtDates = new Y.DataTable({
95
caption: "Data formatting with DataType.Date"
102
<h2>Formatting Row Data for Display</h2>
104
<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".
106
<p>Simple formatting can be defined with a string template on the column definition.</p>
108
<pre class="code prettyprint">YUI().use("datatable", function(Y) {
110
var table = new Y.DataTable({
111
columns: [ "id", "name", { key: "price", formatter: "${value}" } ],
113
{ id: "ga-3475", name: "gadget", price: 6.99 },
114
{ id: "sp-9980", name: "sprocket", price: 3.75 },
115
{ id: "wi-0650", name: "widget", price: 4.25 }
117
caption: "Data formatting with string template"
119
}).render("#template");</pre>
123
When a calculation is needed, define a custom function that generates
124
markup for the data cell. The custom formatter function receives an object
125
with the properties listed in <a href="index.html#formatter-props">Appendix
126
B</a> in the DataTable user guide.
129
<pre class="code prettyprint">// See the DataTable user guide for a list of properties on o.
130
function calculate(o) {
131
return "$" + (o.data.price - o.data.cost).toFixed(2);
134
var table = new Y.DataTable({
135
columns: [ "id", "name", { key: "profit", formatter: calculate } ],
137
{ id: "ga-3475", name: "gadget", price: 6.99, cost: 4.99 },
138
{ id: "sp-9980", name: "sprocket", price: 3.75, cost: 2.75 },
139
{ id: "wi-0650", name: "widget", price: 4.25, cost: 3.25 }
141
caption: "Data formatting with custom function"
142
}).render("#function");</pre>
145
<p>The DataType utility can be used to help format date objects. This example
146
also uses the <code>emptyCellValue</code> column configuration to supply a custom cell
147
value in the case of missing data.</p>
149
<pre class="code prettyprint">YUI().use("datatype-date", "datatable", function (Y) {
150
function formatDates(o) {
151
return o.value &&
152
Y.DataType.Date.format(o.value, { format: "%m/%d/%Y" });
155
dt = new Y.DataTable({
159
{ key: "date", formatter: formatDates, emptyCellValue: "(unknown)" }
162
{ id: "ga-3475", name: "gadget", date: new Date(2006, 5, 1) },
163
{ id: "sp-9980", name: "sprocket", date: new Date(2004, 8, 16) },
164
{ id: "wi-0650", name: "widget"} // no date for this record
166
caption: "Data formatting with DataType.Date"
167
}).render("#dates");</pre>
173
<div class="yui3-u-1-4">
174
<div class="sidebar">
178
<div class="sidebox">
180
<h2 class="no-toc">Examples</h2>
184
<ul class="examples">
187
<li data-description="This example illustrates simple DataTable use cases.">
188
<a href="datatable-basic.html">Basic DataTable</a>
193
<li data-description="DataTable loaded with JSON data from a remote webservice via DataSource.Get">
194
<a href="datatable-dsget.html">DataTable + DataSource.Get + JSON Data</a>
199
<li data-description="DataTable loaded with XML data from a remote webservice via DataSource.IO.">
200
<a href="datatable-dsio.html">DataTable + DataSource.IO + XML Data</a>
205
<li data-description="Custom format data for display.">
206
<a href="datatable-formatting.html">Formatting Row Data for Display</a>
211
<li data-description="DataTable with nested column headers.">
212
<a href="datatable-nestedcols.html">Nested Column Headers</a>
217
<li data-description="DataTable with column sorting.">
218
<a href="datatable-sort.html">Column Sorting</a>
223
<li data-description="DataTable with vertical and/or horizontal scrolling rows.">
224
<a href="datatable-scroll.html">Scrolling DataTable</a>
229
<li data-description="Using DataTable's recordType attribute to create calculated, sortable columns.">
230
<a href="datatable-recordtype.html">Sortable generated columns</a>
242
<div class="sidebox">
244
<h2 class="no-toc">Examples That Use This Component</h2>
248
<ul class="examples">
267
<li data-description="Shows how to instantiate multiple Panel instances, and use nested modality to interact with a Datatable.">
268
<a href="../panel/panel-form.html">Creating a Modal Form</a>
281
<script src="../assets/vendor/prettify/prettify-min.js"></script>
282
<script>prettyPrint();</script>