|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: DataTable + DataSource.IO + XML Data</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: DataTable + DataSource.IO + XML Data</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><div class="intro"> |
|
|
29 |
<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> |
|
|
30 |
|
|
|
31 |
<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> |
|
|
32 |
|
|
|
33 |
<p><strong>Note:</strong> XML parsing currently has known issues on the Android WebKit browser. |
|
|
34 |
</div> |
|
|
35 |
|
|
|
36 |
<div class="example yui3-skin-sam"> |
|
|
37 |
<style scoped> |
|
|
38 |
/* custom styles for this example */ |
|
|
39 |
.example .yui3-datatable { |
|
|
40 |
margin-bottom: 1em; |
|
|
41 |
} |
|
|
42 |
|
|
|
43 |
/* css to counter global site css */ |
|
|
44 |
.example table { |
|
|
45 |
width: auto; |
|
|
46 |
} |
|
|
47 |
.example caption { |
|
|
48 |
display: table-caption; |
|
|
49 |
} |
|
|
50 |
.example th, |
|
|
51 |
.example td { |
|
|
52 |
text-transform: none; |
|
|
53 |
border: 0 none; |
|
|
54 |
} |
|
|
55 |
</style> |
|
|
56 |
|
|
|
57 |
<div id="chinese" class="dt-example"></div> |
|
|
58 |
|
|
|
59 |
<script> |
|
|
60 |
YUI().use("datasource-io", "datasource-xmlschema", "datatable-datasource", function (Y) { |
|
|
61 |
var ds = new Y.DataSource.IO({ |
|
|
62 |
source:"../assets/datatable/ylocal.xml?"}) |
|
|
63 |
.plug(Y.Plugin.DataSourceXMLSchema, { |
|
|
64 |
schema: { |
|
|
65 |
resultListLocator: "Result", |
|
|
66 |
resultFields: [ |
|
|
67 |
{ |
|
|
68 |
key:"Title", |
|
|
69 |
locator:"*[local-name() ='Title']" |
|
|
70 |
}, |
|
|
71 |
{ |
|
|
72 |
key:"Phone", |
|
|
73 |
locator:"*[local-name() ='Phone']" |
|
|
74 |
}, |
|
|
75 |
{ |
|
|
76 |
key:"Rating", |
|
|
77 |
locator:"*[local-name()='Rating']/*[local-name()='AverageRating']", |
|
|
78 |
// The data contains "NaN" for unrated restaurants |
|
|
79 |
parser: function (val) { |
|
|
80 |
return isNaN(val) ? '(none)' : val; |
|
|
81 |
} |
|
|
82 |
} |
|
|
83 |
] |
|
|
84 |
} |
|
|
85 |
}), |
|
|
86 |
dt = new Y.DataTable({ |
|
|
87 |
columns: ["Title", "Phone", "Rating"], |
|
|
88 |
summary:"Chinese restaurants near 98089", |
|
|
89 |
caption:"Table with XML data from same-domain script" |
|
|
90 |
}).plug(Y.Plugin.DataTableDataSource, { |
|
|
91 |
datasource: ds, |
|
|
92 |
initialRequest:"zip=94089&query=chinese" |
|
|
93 |
}).render("#chinese"); |
|
|
94 |
|
|
|
95 |
dt.datasource.load(); |
|
|
96 |
}); |
|
|
97 |
</script> |
|
|
98 |
|
|
|
99 |
</div> |
|
|
100 |
|
|
|
101 |
<h2>Populating Your DataTable with Remote Data using DataSource.IO</h2> |
|
|
102 |
<p> |
|
|
103 |
<strong>Note:</strong> be sure to add the <code>yui3-skin-sam</code> classname to the |
|
|
104 |
page's <code><body></code> element or to a parent element of the widget in order to apply |
|
|
105 |
the default CSS skin. See <a href="http://yuilibrary.com/yui/docs/tutorials/skins/">Understanding Skinning</a>. |
|
|
106 |
</p> |
|
|
107 |
<pre class="code prettyprint"><body class="yui3-skin-sam"> <!-- You need this skin class --></pre> |
|
|
108 |
|
|
|
109 |
<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> |
|
|
110 |
|
|
|
111 |
<p>Start with the <code>use()</code> statement:</p> |
|
|
112 |
|
|
|
113 |
<pre class="code prettyprint">YUI().use("datasource-io", "datasource-xmlschema", "datatable-datasource", function(Y) { |
|
|
114 |
});</pre> |
|
|
115 |
|
|
|
116 |
|
|
|
117 |
<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> |
|
|
118 |
|
|
|
119 |
<pre class="code prettyprint">var dataSource = new Y.DataSource.IO({ |
|
|
120 |
source:"ylocal_proxy.php?zip=94089&query=chinese" |
|
|
121 |
}); |
|
|
122 |
|
|
|
123 |
dataSource.plug(Y.Plugin.DataSourceXMLSchema, { |
|
|
124 |
schema: { |
|
|
125 |
resultListLocator: "Result", |
|
|
126 |
resultFields: [ |
|
|
127 |
{key:"Title", locator:"*[local-name() ='Title']"}, |
|
|
128 |
{key:"Phone", locator:"*[local-name() ='Phone']"}, |
|
|
129 |
{key:"Rating", locator:"*[local-name()='Rating']/*[local-name()='AverageRating']"} |
|
|
130 |
] |
|
|
131 |
} |
|
|
132 |
}); |
|
|
133 |
|
|
|
134 |
dataSource.sendRequest({ |
|
|
135 |
callback: { |
|
|
136 |
success: function (e) { |
|
|
137 |
Y.log(e); |
|
|
138 |
} |
|
|
139 |
} |
|
|
140 |
});</pre> |
|
|
141 |
|
|
|
142 |
|
|
|
143 |
<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> |
|
|
144 |
|
|
|
145 |
<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> |
|
|
146 |
|
|
|
147 |
<pre class="code prettyprint">var dataSource = new Y.DataSource.IO({ |
|
|
148 |
source:"ylocal_proxy.php?zip=94089&query=chinese" |
|
|
149 |
}); |
|
|
150 |
|
|
|
151 |
dataSource.plug(Y.Plugin.DataSourceXMLSchema, { |
|
|
152 |
schema: { |
|
|
153 |
resultListLocator: "Result", |
|
|
154 |
resultFields: [ |
|
|
155 |
{key:"Title", locator:"*[local-name() ='Title']"}, |
|
|
156 |
{key:"Phone", locator:"*[local-name() ='Phone']"}, |
|
|
157 |
{key:"Rating", locator:"*[local-name()='Rating']/*[local-name()='AverageRating']"} |
|
|
158 |
] |
|
|
159 |
} |
|
|
160 |
}); |
|
|
161 |
|
|
|
162 |
var table = new Y.DataTable({ |
|
|
163 |
columns: ["Title", "Phone", "Rating"], |
|
|
164 |
summary: "Chinese restaurants near 98089", |
|
|
165 |
caption: "Table with XML data from same-domain script" |
|
|
166 |
}); |
|
|
167 |
table.plug(Y.Plugin.DataTableDataSource, { |
|
|
168 |
datasource: dataSource, |
|
|
169 |
initialRequest: "" |
|
|
170 |
}); |
|
|
171 |
|
|
|
172 |
dataSource.after("response", function() { |
|
|
173 |
table.render("#pizza")} |
|
|
174 |
);</pre> |
|
|
175 |
|
|
|
176 |
|
|
|
177 |
<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. |
|
|
178 |
|
|
|
179 |
<p>Here's the code to run the whole example:</p> |
|
|
180 |
|
|
|
181 |
<pre class="code prettyprint">YUI().use("datasource-get", "datasource-jsonschema", "datatable-datasource", function(Y) { |
|
|
182 |
var dataSource = new Y.DataSource.IO({ |
|
|
183 |
source: "ylocal_proxy.php?" |
|
|
184 |
}); |
|
|
185 |
|
|
|
186 |
dataSource.plug(Y.Plugin.DataSourceXMLSchema, { |
|
|
187 |
schema: { |
|
|
188 |
resultListLocator: "Result", |
|
|
189 |
resultFields: [ |
|
|
190 |
{key:"Title", locator:"*[local-name() ='Title']"}, |
|
|
191 |
{key:"Phone", locator:"*[local-name() ='Phone']"}, |
|
|
192 |
{key:"Rating", locator:"*[local-name()='Rating']/*[local-name()='AverageRating']"} |
|
|
193 |
] |
|
|
194 |
} |
|
|
195 |
}); |
|
|
196 |
|
|
|
197 |
var table = new Y.DataTable({ |
|
|
198 |
columns: ["Title", "Phone", "Rating"], |
|
|
199 |
summary: "Chinese restaurants near 98089", |
|
|
200 |
caption: "Table with XML data from same-domain script" |
|
|
201 |
}); |
|
|
202 |
|
|
|
203 |
table.plug(Y.Plugin.DataTableDataSource, { |
|
|
204 |
datasource: dataSource, |
|
|
205 |
initialRequest: "zip=94089&query=chinese" |
|
|
206 |
}); |
|
|
207 |
|
|
|
208 |
dataSource.after("response", function() { |
|
|
209 |
table.render("#pizza")} |
|
|
210 |
); |
|
|
211 |
|
|
|
212 |
// Make another request later |
|
|
213 |
//table.datasource.load({request:"zip=94089&query=pizza"}); |
|
|
214 |
});</pre> |
|
|
215 |
|
|
|
216 |
</div> |
|
|
217 |
</div> |
|
|
218 |
</div> |
|
|
219 |
|
|
|
220 |
<div class="yui3-u-1-4"> |
|
|
221 |
<div class="sidebar"> |
|
|
222 |
|
|
|
223 |
|
|
|
224 |
|
|
|
225 |
<div class="sidebox"> |
|
|
226 |
<div class="hd"> |
|
|
227 |
<h2 class="no-toc">Examples</h2> |
|
|
228 |
</div> |
|
|
229 |
|
|
|
230 |
<div class="bd"> |
|
|
231 |
<ul class="examples"> |
|
|
232 |
|
|
|
233 |
|
|
|
234 |
<li data-description="This example illustrates simple DataTable use cases."> |
|
|
235 |
<a href="datatable-basic.html">Basic DataTable</a> |
|
|
236 |
</li> |
|
|
237 |
|
|
|
238 |
|
|
|
239 |
|
|
|
240 |
<li data-description="DataTable loaded with JSON data from a remote webservice via DataSource.Get"> |
|
|
241 |
<a href="datatable-dsget.html">DataTable + DataSource.Get + JSON Data</a> |
|
|
242 |
</li> |
|
|
243 |
|
|
|
244 |
|
|
|
245 |
|
|
|
246 |
<li data-description="DataTable loaded with XML data from a remote webservice via DataSource.IO."> |
|
|
247 |
<a href="datatable-dsio.html">DataTable + DataSource.IO + XML Data</a> |
|
|
248 |
</li> |
|
|
249 |
|
|
|
250 |
|
|
|
251 |
|
|
|
252 |
<li data-description="Custom format data for display."> |
|
|
253 |
<a href="datatable-formatting.html">Formatting Row Data for Display</a> |
|
|
254 |
</li> |
|
|
255 |
|
|
|
256 |
|
|
|
257 |
|
|
|
258 |
<li data-description="DataTable with nested column headers."> |
|
|
259 |
<a href="datatable-nestedcols.html">Nested Column Headers</a> |
|
|
260 |
</li> |
|
|
261 |
|
|
|
262 |
|
|
|
263 |
|
|
|
264 |
<li data-description="DataTable with column sorting."> |
|
|
265 |
<a href="datatable-sort.html">Column Sorting</a> |
|
|
266 |
</li> |
|
|
267 |
|
|
|
268 |
|
|
|
269 |
|
|
|
270 |
<li data-description="DataTable with vertical and/or horizontal scrolling rows."> |
|
|
271 |
<a href="datatable-scroll.html">Scrolling DataTable</a> |
|
|
272 |
</li> |
|
|
273 |
|
|
|
274 |
|
|
|
275 |
|
|
|
276 |
<li data-description="Using DataTable's recordType attribute to create calculated, sortable columns."> |
|
|
277 |
<a href="datatable-recordtype.html">Sortable generated columns</a> |
|
|
278 |
</li> |
|
|
279 |
|
|
|
280 |
|
|
|
281 |
|
|
|
282 |
<li data-description="Populating one DataTable from details in the data of another."> |
|
|
283 |
<a href="datatable-masterdetail.html">Master and detail tables</a> |
|
|
284 |
</li> |
|
|
285 |
|
|
|
286 |
|
|
|
287 |
|
|
|
288 |
<li data-description="Checkbox column that retains checked state when sorting."> |
|
|
289 |
<a href="datatable-chkboxselect.html">Checkbox select column</a> |
|
|
290 |
</li> |
|
|
291 |
|
|
|
292 |
|
|
|
293 |
|
|
|
294 |
|
|
|
295 |
</ul> |
|
|
296 |
</div> |
|
|
297 |
</div> |
|
|
298 |
|
|
|
299 |
|
|
|
300 |
|
|
|
301 |
<div class="sidebox"> |
|
|
302 |
<div class="hd"> |
|
|
303 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
304 |
</div> |
|
|
305 |
|
|
|
306 |
<div class="bd"> |
|
|
307 |
<ul class="examples"> |
|
|
308 |
|
|
|
309 |
|
|
|
310 |
|
|
|
311 |
|
|
|
312 |
|
|
|
313 |
|
|
|
314 |
|
|
|
315 |
|
|
|
316 |
|
|
|
317 |
|
|
|
318 |
|
|
|
319 |
|
|
|
320 |
|
|
|
321 |
|
|
|
322 |
|
|
|
323 |
|
|
|
324 |
|
|
|
325 |
|
|
|
326 |
|
|
|
327 |
|
|
|
328 |
|
|
|
329 |
|
|
|
330 |
<li data-description="Shows how to instantiate multiple Panel instances, and use nested modality to interact with a Datatable."> |
|
|
331 |
<a href="../panel/panel-form.html">Creating a Modal Form</a> |
|
|
332 |
</li> |
|
|
333 |
|
|
|
334 |
|
|
|
335 |
</ul> |
|
|
336 |
</div> |
|
|
337 |
</div> |
|
|
338 |
|
|
|
339 |
</div> |
|
|
340 |
</div> |
|
|
341 |
</div> |
|
|
342 |
</div> |
|
|
343 |
|
|
|
344 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
345 |
<script>prettyPrint();</script> |
|
|
346 |
|
|
|
347 |
<script> |
|
|
348 |
YUI.Env.Tests = { |
|
|
349 |
examples: [], |
|
|
350 |
project: '../assets', |
|
|
351 |
assets: '../assets/datatable', |
|
|
352 |
name: 'datatable-dsio', |
|
|
353 |
title: 'DataTable + DataSource.IO + XML Data', |
|
|
354 |
newWindow: '', |
|
|
355 |
auto: false |
|
|
356 |
}; |
|
|
357 |
YUI.Env.Tests.examples.push('datatable-basic'); |
|
|
358 |
YUI.Env.Tests.examples.push('datatable-dsget'); |
|
|
359 |
YUI.Env.Tests.examples.push('datatable-dsio'); |
|
|
360 |
YUI.Env.Tests.examples.push('datatable-formatting'); |
|
|
361 |
YUI.Env.Tests.examples.push('datatable-nestedcols'); |
|
|
362 |
YUI.Env.Tests.examples.push('datatable-sort'); |
|
|
363 |
YUI.Env.Tests.examples.push('datatable-scroll'); |
|
|
364 |
YUI.Env.Tests.examples.push('datatable-recordtype'); |
|
|
365 |
YUI.Env.Tests.examples.push('datatable-masterdetail'); |
|
|
366 |
YUI.Env.Tests.examples.push('datatable-chkboxselect'); |
|
|
367 |
YUI.Env.Tests.examples.push('panel-form'); |
|
|
368 |
|
|
|
369 |
</script> |
|
|
370 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
371 |
|
|
|
372 |
|
|
|
373 |
|
|
|
374 |
</body> |
|
|
375 |
</html> |