5
<title>DataTable (deprecated version)</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>DataTable (deprecated version)</h1>
18
<a href="#toc" class="jump">Jump to Table of Contents</a>
22
<div class="yui3-u-3-4">
24
<div class="content"><style scoped>
27
border: 1px solid #eac9a9;
28
-moz-border-radius: 3px;
29
-webkit-border-radius: 3px;
32
-moz-box-shadow: 0 0 5px #ccc8b3;
33
-webkit-box-shadow: 0 0 5px #ccc8b3;
34
box-shadow: 0 0 5px #ccc8b3;
38
<div class="intro component">
40
The DataTable widget is responsible for rendering columnar data into a
41
highly customizable and fully accessible HTML table. The core
42
functionality of DataTable is to visualize structured data as a table.
43
A variety of Plugins can then be used to add features to the table such
44
as sorting and scrolling.
50
<strong>NOTICE</strong>: This component is <strong>deprecated</strong>
51
and will be removed in future versions. Use
52
<a href="../datatable/index.html">the latest supported version of
57
This component will be temporarily supported for implementations that
58
have unresolvable issues preventing them from upgrading to the new
59
APIs. If you find issues while updating to the latest version, please
60
<a href="../../../projects/yui3/newticket/">file a ticket</a>.
64
This module is <strong>incompatible</strong> with
65
<a href="../datatable/index.html">the new <code>datatable</code> component
66
suite</a>. Both use the <code>Y.DataTable</code> namespace.
70
Read the <a href="../datatable/migration.html">Migration Guide</a>
71
for help upgrading from version 3.4.1 or prior.
75
<h2 id="getting-started">Getting Started</h2>
78
To include the source files for DataTable (deprecated version) and its dependencies, first load
79
the YUI seed file if you haven't already loaded it.
82
<pre class="code prettyprint"><script src="http://yui.yahooapis.com/3.5.0/build/yui/yui-min.js"></script></pre>
86
Next, create a new YUI instance for your application and populate it with the
87
modules you need by specifying them as arguments to the <code>YUI().use()</code> method.
88
YUI will automatically load any dependencies required by the modules you
92
<pre class="code prettyprint"><script>
93
// Create a new YUI instance and populate it with the required modules.
94
YUI().use('datatable-deprecated', function (Y) {
95
// DataTable (deprecated version) is available and ready for use. Add implementation
96
// code here.
98
</script></pre>
102
For more information on creating YUI instances and on the
103
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the
104
documentation for the <a href="../yui/index.html">YUI Global Object</a>.
108
<h2 id="using">Using DataTables</h2>
110
<h3 id="basics">DataTable basics</h3>
113
A basic DataTable is comprised of columns and rows. Define the columns you
114
want to display in your DataTable with the <code>columnset</code>
115
attribute. Rows are created for you based on the data you define using the
116
<code>recordset</code> attribute. Under the hood, the DataTable class uses
117
a Columnset instance and a Recordset instance to manage column and row data
121
<h3 id="columns">Working with columns</h3>
124
A <code>columnset</code> can be defined with a simple array of
125
<code>keys</code>. As long as these keys exist in your data, DataTable will
126
display these columns of data in the table. Note that your data may contain
127
other columns that are not displayed if they are not defined in the
128
<code>columnset</code> array. A Columnset will be created containing a
129
Column instance for each item in your array, with the <code>key</code>
133
<pre class="code prettyprint">// Creates a Columnset with 3 Columns. "cost" is not rendered.
134
var cols = ["id","name","price"];
136
// Columns must match data parameter names
138
{id:"ga-3475", name:"gadget", price:"$6.99", cost:"$5.99"},
139
{id:"sp-9980", name:"sprocket", price:"$3.75", cost:"$3.25"},
140
{id:"wi-0650", name:"widget", price:"$4.25", cost:"$3.75"}
143
// Creates a DataTable with 3 columns and 3 rows
144
var table = new Y.DataTable.Base({
147
}).render("#example");</pre>
151
For greater flexibility, the <code>columnset</code> array accepts
152
configuration objects as well as simple column name strings. When
153
identifying a column with a configuration object, use the <code>key</code>
154
property to reference the column name string. Below are a few other
155
available column configurations.
159
Use the <code>label</code> attribute to customize the rendered column
163
<pre class="code prettyprint">// The label is the text that will be rendered in the table head
165
{ key: "id", label: "ID" },
166
{ key: "name", label: "Name" },
167
{ key: "price", label: "Price" }
172
Use the <code>emptyCellValue</code> attribute to provide custom cell content when
173
there is no data for that cell in the <code>Recordset</code> or a <code>formatter</code> returns
174
<code>undefined</code>. The default <code>emptyCellValue</code> is the empty string <code>""</code>.
177
<pre class="code prettyprint">// The label is the text that will be rendered in the table head
179
{ key: "id", label: "ID" },
180
{ key: "name", label: "Name" },
181
{ key: "price", label: "Price", emptyCellValue: "<em>FREE!!!</em>" }
186
Use the <code>abbr</code> attribute to set the screen-reader friendly
187
"abbr" on each TH element:
190
<pre class="code prettyprint">// The abbr attribute enhances the screen-reader experience
193
key: "mfr-parts-database-id",
194
label: "Mfr Part ID",
198
key: "mfr-parts-database-name",
199
label: "Mfr Part Name",
200
abbr: "Name"
203
key: "mfr-parts-database-price",
204
label: "Wholesale Price",
205
abbr: "Price"
211
Use the <code>children</code> attribute to created nested column headers.
212
Parent columns are for display purposes only, not associated with any data,
213
and should not have a <code>key</code> attribute of their own.
216
<pre class="code prettyprint">var nestedCols = [
218
label: "Train Schedule",
219
children: [ // Use children to define nested relationships
220
{ key: "track" },
222
label: "Route",
224
{ key: "from" },
225
{ key: "to" }
233
{ track: "1", from: "Paris", to: "Amsterdam" },
234
{ track: "2", from: "Paris", to: "London" },
235
{ track: "3", from: "Paris", to: "Zurich" }
238
var table = new Y.DataTable.Base({
239
columnset: nestedCols,
241
summary: "Train schedule",
242
caption: "Table with nested column headers"
243
}).render("#nested");</pre>
246
<h3 id="data">Working with row data</h3>
249
Pass an array of data to the <code>recordset</code> attribute, and
250
DataTable will create a <code>Recordset</code> of <code>Record</code>
251
instances to populate the table. Note that you should only define columns
252
for data you want to display -- all other data is not displayed.
255
<pre class="code prettyprint">// Creates a Recordset with 3 records
257
{ id: "ga-3475", name: "gadget", price: "$6.99", cost: "$5.99"},
258
{ id: "sp-9980", name: "sprocket", price: "$3.75", cost: "$3.25"},
259
{ id: "wi-0650", name: "widget", price: "$4.25", cost: "$3.75"}
262
// Creates a DataTable with 3 columns and 3 rows ("cost" is not displayed)
263
var table = new Y.DataTable.Base({
264
columnset: [ "id", "name", "price" ],
266
}).render("#example");</pre>
270
Data can be stored in one format but be displayed in a different format.
271
For instance, prices can be stored as numbers but be displayed as "$2.99",
272
and birthdays can be stored as date objects but be displayed as
273
"12/9/2009". Simple formatting can be defined with a string template on the
277
<pre class="code prettyprint">var cols = [ "id", "name", { key: "price", formatter: "\${value}" } ];
280
{ id: "ga-3475", name: "gadget", price: 6.99 },
281
{ id: "sp-9980", name: "sprocket", price: 3.75 },
282
{ id: "wi-0650", name: "widget", price: 4.25 }
285
var table = new Y.DataTable.Base({
288
caption: "Data formatting with string template"
289
}).render("#template");</pre>
293
When a calculation is needed, define a custom function that generates
294
markup for the cell. The custom formatter function receives an object with
295
the following properties:
307
<td><code>tbody</code></td>
308
<td>The <code><tbody></code> node containing the cell.</td>
311
<td><code>tr</code></td>
312
<td>The <code><tr></code> node containing the cell.</td>
315
<td><code>td</code></td>
316
<td>The cell <code><td></code> node. As of 3.4.1, this property
317
is <strong>not provided by default</strong>, but can be generated by the
318
<a href="http://yuilibrary.com/yui/docs/api/classes/DataTable.html#methods_createCell">createCell</a>
322
<td><code>value</code></td>
323
<td>Usually the value stored in the <code>Record</code> for the column. This is the default content that will be displayed.</td>
326
<td><code>record</code></td>
327
<td>The <code>Record</code> instance containing the data for all cells in the row.</td>
330
<td><code>data</code></td>
331
<td>The raw data collection from the <code>Record</code> instance.</td>
334
<td><code>rowindex</code></td>
335
<td>The row number of the <code><tr></code> node containing the cell (zero based).</td>
338
<td><code>column</code></td>
339
<td>The <code>Column</code> instance for the cell's column.</td>
342
<td><code>classnames</code></td>
343
<td>The classname corresponding to the ID of the cell's column.</td>
346
<td><code>headers</code></td>
347
<td>The Array of IDs from all <code><th></code>s corresponding to the cell (mostly relevant to nested headers).</td>
352
<pre class="code prettyprint">// The custom formatter function recieves an object
353
var calculate = function (o) {
354
var cost = o.record.getValue("cost"),
355
price = o.record.getValue("price");
357
return "$" + (price - cost).toFixed(2);
360
// Assign the custom formatter in the column definition
361
var cols = [ "id", "name", { key: "profit", formatter: calculate } ];
364
{ id: "ga-3475", name: "gadget", price: 6.99, cost: 4.99 },
365
{ id: "sp-9980", name: "sprocket", price: 3.75, cost: 2.75 },
366
{ id: "wi-0650", name: "widget", price: 4.25, cost: 3.25 }
369
var table = new Y.DataTable.Base({
372
caption: "Data formatting with custom function"
373
}).render("#function");</pre>
377
Integrate with the <a href="../datasource/">DataSource</a> data abstraction
378
utility to easily load data from remote sources and implement features such
379
as caching and polling.
382
<pre class="code prettyprint">var cols = [
385
{ key: "Rating.AverageRating", label: "Rating" }
388
var myDataSource = new Y.DataSource.Get({
389
source: "http://query.yahooapis.com/v1/public/yql?&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
392
myDataSource.plug(Y.Plugin.DataSourceJSONSchema, {
394
resultListLocator: "query.results.Result",
395
resultFields: [ "Title", "Phone", "Rating.AverageRating" ]
399
var table = new Y.DataTable.Base({
401
summary: "Pizza places near 98089"
404
table.plug(Y.Plugin.DataTableDataSource, {
405
datasource: myDataSource
408
table.render("#pizza");
410
// Load the data into the table
411
table.datasource.load({
412
request: "&q=select%20*%20from%20local.search%20where%20zip%3D%2794089%27%20and%20query%3D%27pizza%27"
415
// Make another request later
416
table.datasource.load({
417
request: "&q=select%20*%20from%20local.search%20where%20zip%3D%2794089%27%20and%20query%3D%27chinese%27"
422
Enable DataSource caching.
425
<pre class="code prettyprint">var cols = [
428
{ key: "Rating.AverageRating", label: "Rating" }
431
var myDataSource = new Y.DataSource.Get({
432
source: "http://query.yahooapis.com/v1/public/yql?format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
436
.plug(Y.Plugin.DataSourceJSONSchema, {
438
resultListLocator: "query.results.Result",
439
resultFields: ["Title", "Phone", "Rating.AverageRating"]
442
.plug(Y.Plugin.DataSourceCache, {
446
var table = new Y.DataTable.Base({
448
summary: "Pizza places near 98089",
449
caption: "Table with JSON data from YQL"
453
.plug(Y.Plugin.DataTableDataSource, {
454
datasource: myDataSource
456
.render("#pizza");
458
table.datasource.load({
459
request: "&q=select%20*%20from%20local.search%20where%20zip%3D%2794089%27%20and%20query%3D%27chinese%27"
464
Enable DataSource polling.
467
<pre class="code prettyprint">var cols = ["Title", "Phone", "Rating"];
469
var myDataSource = new Y.DataSource.IO({
470
source: "/path/to/service.php?"
473
myDataSource.plug(Y.Plugin.DataSourceXMLSchema, {
475
resultListLocator: "Result",
477
{ key: "Title", locator: "*[local-name() ='Title']" },
478
{ key: "Phone", locator: "*[local-name() ='Phone']" },
479
{ key: "Rating", locator: "*[local-name()='Rating']/*[local-name()='AverageRating']" }
484
var table = new Y.DataTable.Base({
486
summary: "Chinese restaurants near 98089",
487
caption: "Table with XML data from same-domain script"
491
.plug(Y.Plugin.DataTableDataSource, {
492
datasource: myDataSource,
493
initialRequest: "zip=94089&query=chinese"
495
.render("#chinese");
497
myDataSource.setInterval(5000, {
498
request: "zip=94089&query=chinese",
500
success: Y.bind(table.datasource.onDataReturnInitializeTable, table.datasource),
501
failure: Y.bind(table.datasource.onDataReturnInitializeTable, table.datasource)
506
<h3 id="sorting">Column sorting</h3>
509
Column sorting functionality for the deprecated version of DataTable can be
510
added with the DataTableSort plugin (provided by the
511
<code>datatable-sort-deprecated</code> module, or in the
512
<code>datatable-deprecated</code> rollup module). Indicate which columns
513
are sortable by setting <code>sortable: true</code> in your column
517
<pre class="code prettyprint">var cols = [
518
{ key: "Company", sortable: true },
519
{ key: "Phone" },
520
{ key: "Contact", sortable: true }
524
{ Company: "Company Bee", Phone: "415-555-1234", Contact: "Sally Spencer"},
525
{ Company: "Acme Company", Phone: "650-555-4444", Contact: "John Jones"},
526
{ Company: "Indutrial Industries", Phone: "408-555-5678", Contact: "Robin Smith"}
529
var table = new Y.DataTable.Base({
532
summary: "Contacts list",
533
caption: "Table with simple column sorting",
534
plugins: [ Y.Plugin.DataTableSort ]
535
}).render("#sort");</pre>
538
<h3 id="scrolling">Scrolling</h3>
541
<strong>Note:</strong> Scrolling is not currently supported on the Android
546
Scrolling functionality for the deprecated version of DataTable can be added with the DataTableScroll plugin (provided by the <code>datatable-scroll-deprecated</code> module or in the <code>datatable-deprecated</code> rollup module). Horizontal scrolling is enabled by setting a <code>width</code> attribute value; fixed header vertical scrolling is enabled by setting a <code>height</code> attribute value; and xy-scrolling is enabled by setting both <code>width</code> and <code>height</code> values.
549
<pre class="code prettyprint">var cols = [
550
{ key: "Company" },
551
{ key: "Phone" },
552
{ key: "Contact" }
556
{ Company: "Company Bee", Phone: "415-555-1234", Contact: "Sally Spencer"},
557
{ Company: "Acme Company", Phone: "650-555-4444", Contact: "John Jones"},
558
{ Company: "Indutrial Industries", Phone: "408-555-5678", Contact: "Robin Smith"}
561
var table = new Y.DataTable.Base({
567
.plug(Y.Plugin.DataTableScroll, {
568
width: "300px",
569
height: "200px"
571
.render("#scroll");</pre>
574
<h2 id="knownissues">Known Issues</h2>
579
<a href="http://yuilibrary.com/projects/yui3/ticket/2529761">not
580
currently supported on Android</a> WebKit browser.
582
<li>Scrolling DataTable does
583
<a href="http://yuilibrary.com/projects/yui3/ticket/2531047">not appear
584
scrollable</a> in iOS and OS X 10.7 in Safari 5.1+ and Chrome 15+.
592
<div class="yui3-u-1-4">
593
<div class="sidebar">
595
<div id="toc" class="sidebox">
597
<h2 class="no-toc">Table of Contents</h2>
603
<a href="#getting-started">Getting Started</a>
606
<a href="#using">Using DataTables</a>
609
<a href="#basics">DataTable basics</a>
612
<a href="#columns">Working with columns</a>
615
<a href="#data">Working with row data</a>
618
<a href="#sorting">Column sorting</a>
621
<a href="#scrolling">Scrolling</a>
626
<a href="#knownissues">Known Issues</a>
634
<div class="sidebox">
636
<h2 class="no-toc">Examples</h2>
640
<ul class="examples">
643
<li data-description="This example illustrates simple DataTable use cases.">
644
<a href="datatable-basic.html">Basic DataTable</a>
649
<li data-description="DataTable loaded with JSON data from a remote webservice via DataSource.Get">
650
<a href="datatable-dsget.html">DataTable + DataSource.Get + JSON Data</a>
655
<li data-description="DataTable loaded with XML data from a remote webservice via DataSource.IO.">
656
<a href="datatable-dsio.html">DataTable + DataSource.IO + XML Data</a>
661
<li data-description="Custom format data for display.">
662
<a href="datatable-formatting.html">Formatting Row Data for Display</a>
667
<li data-description="DataTable with nested column headers.">
668
<a href="datatable-nestedcols.html">Nested Column Headers</a>
673
<li data-description="DataTable with column sorting.">
674
<a href="datatable-sort.html">Column Sorting</a>
679
<li data-description="DataTable with vertical and/or horizontal scrolling rows.">
680
<a href="datatable-scroll.html">Scrolling DataTable</a>
695
<script src="../assets/vendor/prettify/prettify-min.js"></script>
696
<script>prettyPrint();</script>