5
<title>Example: DataTable + DataSource.IO + XML Data</title>
6
<link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
7
<link rel="stylesheet" href="../assets/css/main.css">
8
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
9
<script src="../../build/yui/yui-min.js"></script>
14
<h1>Example: DataTable + DataSource.IO + XML Data</h1>
19
<div id="main" class="yui3-u">
20
<div class="content"><div class="intro">
21
<p>This example shows how to populate a DataTable with data from the Yahoo! Local webservice retrieved via a same-domain script. First we create a DataSource.IO instance pointing to data, then using the DataTableDataSource plugin we can load data for Chinese restaurants near our office.</p>
23
<p>In this example, we set the <code>initialRequest</code> value in the DataTableDataSource plugin constructor so that the initial data is loaded first and then the DataTable is rendered in a separate call.</p>
25
<p><strong>Note:</strong> XML parsing currently has known issues on the Android WebKit browser.
28
<div class="example yui3-skin-sam">
30
/* custom styles for this example */
31
.dt-example {margin:1em;}
33
/* css to counter global site css */
34
.dt-example th {text-transform:none;}
35
.dt-example table {width:auto;}
36
.dt-example caption {display:table-caption;}
39
<div id="chinese" class="dt-example"></div>
41
<script type="text/javascript">
42
YUI().use("datasource-io", "datasource-xmlschema","datatable-base","datatable-datasource", function (Y) {
43
var cols = ["Title", "Phone", "Rating"];
44
ds = new Y.DataSource.IO({
45
source:"../assets/datatable/ylocal.xml?"})
46
.plug(Y.Plugin.DataSourceXMLSchema, {
48
resultListLocator: "Result",
52
locator:"*[local-name() ='Title']"
56
locator:"*[local-name() ='Phone']"
60
locator:"*[local-name()='Rating']/*[local-name()='AverageRating']",
61
// The data contains "NaN" for unrated restaurants
62
parser: function (val) {
63
return isNaN(val) ? '(none)' : val;
69
dt = new Y.DataTable.Base({columnset:cols, summary:"Chinese restaurants near 98089", caption:"Table with XML data from same-domain script"})
70
.plug(Y.Plugin.DataTableDataSource, {datasource:ds, initialRequest:"zip=94089&query=chinese"});
72
ds.after("response", function() {
73
dt.render("#chinese")}
80
<h2>Populating Your DataTable with Remote Data using DataSource.IO</h2>
82
<p>Your table can be easily populated with XML data from your back-end by creating a DataSource instance and using the DataTableDataSource plugin to load the data into a DataTable.</p>
84
<p>Start with the <code>use()</code> statement:</p>
86
<pre class="code prettyprint">YUI().use("datasource-io", "datasource-xmlschema", "datatable-datasource", function(Y) {
90
<p>Next create a DataSource.IO instance pointing to your data. (Note that in order to point the Yahoo! Local webservice, you would need to bypass cross-domain browser restrictions on XHR by creating a locally served proxy. You do not need to implement such a proxy when your data is accessed from the same domain.) Define the correct schema for the DataSourceJSONSchema plugin. This is a good time to call <code>sendRequest</code> to make sure the data returns as expected.</p>
92
<pre class="code prettyprint">var dataSource = new Y.DataSource.IO({
93
source:"ylocal_proxy.php?zip=94089&query=chinese"
96
dataSource.plug(Y.Plugin.DataSourceXMLSchema, {
98
resultListLocator: "Result",
100
{key:"Title", locator:"*[local-name() ='Title']"},
101
{key:"Phone", locator:"*[local-name() ='Phone']"},
102
{key:"Rating", locator:"*[local-name()='Rating']/*[local-name()='AverageRating']"}
107
dataSource.sendRequest({
109
success: function (e) {
116
<p>Now that the DataSource is created properly, define the columns that you want your table to show. These columns map directly to the parameter names returned in the data.</p>
118
<pre class="code prettyprint">var cols = ["Title", "Phone", "Rating"];</pre>
121
<p>Now you are ready to create a DataTable using the columns you have defined. When you plug the instance with the DataTableDataSource plugin, point to your DataSource instance, and set an <code>initialRequest</code> value so that the initial data loads right way. Then call <code>render()</code> after the data response has been processed.</p>
123
<pre class="code prettyprint">var cols = ["Title", "Phone", "Rating"];
125
var dataSource = new Y.DataSource.IO({
126
source:"ylocal_proxy.php?zip=94089&query=chinese"
129
dataSource.plug(Y.Plugin.DataSourceXMLSchema, {
131
resultListLocator: "Result",
133
{key:"Title", locator:"*[local-name() ='Title']"},
134
{key:"Phone", locator:"*[local-name() ='Phone']"},
135
{key:"Rating", locator:"*[local-name()='Rating']/*[local-name()='AverageRating']"}
140
var table = new Y.DataTable.Base({
142
summary: "Chinese restaurants near 98089",
143
caption: "Table with XML data from same-domain script"
145
table.plug(Y.Plugin.DataTableDataSource, {
146
datasource: dataSource,
147
initialRequest: ""
150
dataSource.after("response", function() {
151
table.render("#pizza")}
155
<p>One final change you can make is to split the URL between the DataSource <code>source</code> value and the <code>request</code> value sent to the DataTableDataSource plugin. Splitting the URL this way facilitates making future requests to the same DataSource.
157
<p>Here's the code to run the whole example:</p>
159
<pre class="code prettyprint">YUI().use("datasource-get", "datasource-jsonschema", "datatable-datasource", function(Y) {
160
var cols = ["Title", "Phone", "Rating"];
162
var dataSource = new Y.DataSource.IO({
163
source: "ylocal_proxy.php?"
166
dataSource.plug(Y.Plugin.DataSourceXMLSchema, {
168
resultListLocator: "Result",
170
{key:"Title", locator:"*[local-name() ='Title']"},
171
{key:"Phone", locator:"*[local-name() ='Phone']"},
172
{key:"Rating", locator:"*[local-name()='Rating']/*[local-name()='AverageRating']"}
177
var table = new Y.DataTable.Base({
179
summary: "Chinese restaurants near 98089",
180
caption: "Table with XML data from same-domain script"
183
table.plug(Y.Plugin.DataTableDataSource, {
184
datasource: dataSource,
185
initialRequest: "zip=94089&query=chinese"
188
dataSource.after("response", function() {
189
table.render("#pizza")}
192
// Make another request later
193
//table.datasource.load({request:"zip=94089&query=pizza"});
199
<div id="sidebar" class="yui3-u">
203
<div class="sidebox">
205
<h2 class="no-toc">Examples</h2>
209
<ul class="examples">
212
<li data-description="This example illustrates simple DataTable use cases.">
213
<a href="datatable-basic.html">Basic DataTable</a>
218
<li data-description="DataTable loaded with JSON data from a remote webservice via DataSource.Get">
219
<a href="datatable-dsget.html">DataTable + DataSource.Get + JSON Data</a>
224
<li data-description="DataTable loaded with XML data from a remote webservice via DataSource.IO.">
225
<a href="datatable-dsio.html">DataTable + DataSource.IO + XML Data</a>
230
<li data-description="Custom format data for display.">
231
<a href="datatable-formatting.html">Formatting Row Data for Display</a>
236
<li data-description="DataTable with nested column headers.">
237
<a href="datatable-nestedcols.html">Nested Column Headers</a>
242
<li data-description="DataTable with column sorting.">
243
<a href="datatable-sort.html">Column Sorting</a>
248
<li data-description="DataTable with vertical and/or horizontal scrolling rows.">
249
<a href="datatable-scroll.html">Scrolling DataTable</a>
261
<div class="sidebox">
263
<h2 class="no-toc">Examples That Use This Component</h2>
267
<ul class="examples">
284
<li data-description="Shows how to instantiate multiple Panel instances, and use nested modality to interact with a Datatable.">
285
<a href="../panel/panel-form.html">Creating a modal form</a>
297
<script src="../assets/vendor/prettify/prettify-min.js"></script>
298
<script>prettyPrint();</script>