5
<title>Example: DataTable + DataSource.IO + XML Data</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: DataTable + DataSource.IO + XML Data</h1>
20
<div class="yui3-u-3-4">
22
<div class="content"><div class="intro">
23
<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>
25
<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>
27
<p><strong>Note:</strong> XML parsing currently has known issues on the Android WebKit browser.
30
<div class="example yui3-skin-sam">
32
/* custom styles for this example */
33
.example .yui3-datatable {
37
/* css to counter global site css */
42
display: table-caption;
51
<div id="chinese" class="dt-example"></div>
54
YUI().use("datasource-io", "datasource-xmlschema", "datatable-datasource", function (Y) {
55
var ds = new Y.DataSource.IO({
56
source:"../assets/datatable/ylocal.xml?"})
57
.plug(Y.Plugin.DataSourceXMLSchema, {
59
resultListLocator: "Result",
63
locator:"*[local-name() ='Title']"
67
locator:"*[local-name() ='Phone']"
71
locator:"*[local-name()='Rating']/*[local-name()='AverageRating']",
72
// The data contains "NaN" for unrated restaurants
73
parser: function (val) {
74
return isNaN(val) ? '(none)' : val;
80
dt = new Y.DataTable({
81
columns: ["Title", "Phone", "Rating"],
82
summary:"Chinese restaurants near 98089",
83
caption:"Table with XML data from same-domain script"
84
}).plug(Y.Plugin.DataTableDataSource, {
86
initialRequest:"zip=94089&query=chinese"
87
}).render("#chinese");
95
<h2>Populating Your DataTable with Remote Data using DataSource.IO</h2>
97
<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>
99
<p>Start with the <code>use()</code> statement:</p>
101
<pre class="code prettyprint">YUI().use("datasource-io", "datasource-xmlschema", "datatable-datasource", function(Y) {
105
<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>
107
<pre class="code prettyprint">var dataSource = new Y.DataSource.IO({
108
source:"ylocal_proxy.php?zip=94089&query=chinese"
111
dataSource.plug(Y.Plugin.DataSourceXMLSchema, {
113
resultListLocator: "Result",
115
{key:"Title", locator:"*[local-name() ='Title']"},
116
{key:"Phone", locator:"*[local-name() ='Phone']"},
117
{key:"Rating", locator:"*[local-name()='Rating']/*[local-name()='AverageRating']"}
122
dataSource.sendRequest({
124
success: function (e) {
131
<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>
133
<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>
135
<pre class="code prettyprint">var dataSource = new Y.DataSource.IO({
136
source:"ylocal_proxy.php?zip=94089&query=chinese"
139
dataSource.plug(Y.Plugin.DataSourceXMLSchema, {
141
resultListLocator: "Result",
143
{key:"Title", locator:"*[local-name() ='Title']"},
144
{key:"Phone", locator:"*[local-name() ='Phone']"},
145
{key:"Rating", locator:"*[local-name()='Rating']/*[local-name()='AverageRating']"}
150
var table = new Y.DataTable({
151
columns: ["Title", "Phone", "Rating"],
152
summary: "Chinese restaurants near 98089",
153
caption: "Table with XML data from same-domain script"
155
table.plug(Y.Plugin.DataTableDataSource, {
156
datasource: dataSource,
157
initialRequest: ""
160
dataSource.after("response", function() {
161
table.render("#pizza")}
165
<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.
167
<p>Here's the code to run the whole example:</p>
169
<pre class="code prettyprint">YUI().use("datasource-get", "datasource-jsonschema", "datatable-datasource", function(Y) {
170
var dataSource = new Y.DataSource.IO({
171
source: "ylocal_proxy.php?"
174
dataSource.plug(Y.Plugin.DataSourceXMLSchema, {
176
resultListLocator: "Result",
178
{key:"Title", locator:"*[local-name() ='Title']"},
179
{key:"Phone", locator:"*[local-name() ='Phone']"},
180
{key:"Rating", locator:"*[local-name()='Rating']/*[local-name()='AverageRating']"}
185
var table = new Y.DataTable({
186
columns: ["Title", "Phone", "Rating"],
187
summary: "Chinese restaurants near 98089",
188
caption: "Table with XML data from same-domain script"
191
table.plug(Y.Plugin.DataTableDataSource, {
192
datasource: dataSource,
193
initialRequest: "zip=94089&query=chinese"
196
dataSource.after("response", function() {
197
table.render("#pizza")}
200
// Make another request later
201
//table.datasource.load({request:"zip=94089&query=pizza"});
208
<div class="yui3-u-1-4">
209
<div class="sidebar">
213
<div class="sidebox">
215
<h2 class="no-toc">Examples</h2>
219
<ul class="examples">
222
<li data-description="This example illustrates simple DataTable use cases.">
223
<a href="datatable-basic.html">Basic DataTable</a>
228
<li data-description="DataTable loaded with JSON data from a remote webservice via DataSource.Get">
229
<a href="datatable-dsget.html">DataTable + DataSource.Get + JSON Data</a>
234
<li data-description="DataTable loaded with XML data from a remote webservice via DataSource.IO.">
235
<a href="datatable-dsio.html">DataTable + DataSource.IO + XML Data</a>
240
<li data-description="Custom format data for display.">
241
<a href="datatable-formatting.html">Formatting Row Data for Display</a>
246
<li data-description="DataTable with nested column headers.">
247
<a href="datatable-nestedcols.html">Nested Column Headers</a>
252
<li data-description="DataTable with column sorting.">
253
<a href="datatable-sort.html">Column Sorting</a>
258
<li data-description="DataTable with vertical and/or horizontal scrolling rows.">
259
<a href="datatable-scroll.html">Scrolling DataTable</a>
264
<li data-description="Using DataTable's recordType attribute to create calculated, sortable columns.">
265
<a href="datatable-recordtype.html">Sortable generated columns</a>
277
<div class="sidebox">
279
<h2 class="no-toc">Examples That Use This Component</h2>
283
<ul class="examples">
302
<li data-description="Shows how to instantiate multiple Panel instances, and use nested modality to interact with a Datatable.">
303
<a href="../panel/panel-form.html">Creating a Modal Form</a>
316
<script src="../assets/vendor/prettify/prettify-min.js"></script>
317
<script>prettyPrint();</script>