datatable-dsio.mustache revision c7aeb2c8479a339ddcc01cf5973c31ddd6277b0d
199767f8919635c4928607450d9e0abb932109ceToomas Soome<div class="intro">
199767f8919635c4928607450d9e0abb932109ceToomas Soome <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>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome <p>In this example, we set the `initialRequest` 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>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome <p><strong>Note:</strong> XML parsing currently has known issues on the Android WebKit browser.
199767f8919635c4928607450d9e0abb932109ceToomas Soome</div>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome<div class="example yui3-skin-sam">
199767f8919635c4928607450d9e0abb932109ceToomas Soome{{>datatable-dsio-source}}
199767f8919635c4928607450d9e0abb932109ceToomas Soome</div>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome<h2>Populating Your DataTable with Remote Data using DataSource.IO</h2>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome<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>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome<p>Start with the `use()` statement:</p>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome```
199767f8919635c4928607450d9e0abb932109ceToomas SoomeYUI().use("datasource-io", "datasource-xmlschema", "datatable-datasource", function(Y) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome});
199767f8919635c4928607450d9e0abb932109ceToomas Soome```
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome<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 `sendRequest` to make sure the data returns as expected.</p>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome```
199767f8919635c4928607450d9e0abb932109ceToomas Soomevar dataSource = new Y.DataSource.IO({
199767f8919635c4928607450d9e0abb932109ceToomas Soome source:"ylocal_proxy.php?zip=94089&query=chinese"
199767f8919635c4928607450d9e0abb932109ceToomas Soome});
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomedataSource.plug(Y.Plugin.DataSourceXMLSchema, {
199767f8919635c4928607450d9e0abb932109ceToomas Soome schema: {
199767f8919635c4928607450d9e0abb932109ceToomas Soome resultListLocator: "Result",
199767f8919635c4928607450d9e0abb932109ceToomas Soome resultFields: [
199767f8919635c4928607450d9e0abb932109ceToomas Soome {key:"Title", locator:"*[local-name() ='Title']"},
199767f8919635c4928607450d9e0abb932109ceToomas Soome {key:"Phone", locator:"*[local-name() ='Phone']"},
199767f8919635c4928607450d9e0abb932109ceToomas Soome {key:"Rating", locator:"*[local-name()='Rating']/*[local-name()='AverageRating']"}
199767f8919635c4928607450d9e0abb932109ceToomas Soome ]
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome});
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomedataSource.sendRequest({
199767f8919635c4928607450d9e0abb932109ceToomas Soome callback: {
199767f8919635c4928607450d9e0abb932109ceToomas Soome success: function (e) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome Y.log(e);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome});
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome```
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome<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>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome<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 `initialRequest` value so that the initial data loads right way. Then call `render()` after the data response has been processed.</p>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome```
199767f8919635c4928607450d9e0abb932109ceToomas Soomevar dataSource = new Y.DataSource.IO({
199767f8919635c4928607450d9e0abb932109ceToomas Soome source:"ylocal_proxy.php?zip=94089&query=chinese"
199767f8919635c4928607450d9e0abb932109ceToomas Soome});
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomedataSource.plug(Y.Plugin.DataSourceXMLSchema, {
199767f8919635c4928607450d9e0abb932109ceToomas Soome schema: {
199767f8919635c4928607450d9e0abb932109ceToomas Soome resultListLocator: "Result",
199767f8919635c4928607450d9e0abb932109ceToomas Soome resultFields: [
199767f8919635c4928607450d9e0abb932109ceToomas Soome {key:"Title", locator:"*[local-name() ='Title']"},
199767f8919635c4928607450d9e0abb932109ceToomas Soome {key:"Phone", locator:"*[local-name() ='Phone']"},
199767f8919635c4928607450d9e0abb932109ceToomas Soome {key:"Rating", locator:"*[local-name()='Rating']/*[local-name()='AverageRating']"}
199767f8919635c4928607450d9e0abb932109ceToomas Soome ]
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome});
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevar table = new Y.DataTable({
199767f8919635c4928607450d9e0abb932109ceToomas Soome columns: ["Title", "Phone", "Rating"],
199767f8919635c4928607450d9e0abb932109ceToomas Soome summary: "Chinese restaurants near 98089",
199767f8919635c4928607450d9e0abb932109ceToomas Soome caption: "Table with XML data from same-domain script"
199767f8919635c4928607450d9e0abb932109ceToomas Soome});
199767f8919635c4928607450d9e0abb932109ceToomas Soometable.plug(Y.Plugin.DataTableDataSource, {
199767f8919635c4928607450d9e0abb932109ceToomas Soome datasource: dataSource,
199767f8919635c4928607450d9e0abb932109ceToomas Soome initialRequest: ""
199767f8919635c4928607450d9e0abb932109ceToomas Soome});
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomedataSource.after("response", function() {
199767f8919635c4928607450d9e0abb932109ceToomas Soome table.render("#pizza")}
199767f8919635c4928607450d9e0abb932109ceToomas Soome);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome```
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome<p>One final change you can make is to split the URL between the DataSource `source` value and the `request` value sent to the DataTableDataSource plugin. Splitting the URL this way facilitates making future requests to the same DataSource.
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome<p>Here's the code to run the whole example:</p>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome```
199767f8919635c4928607450d9e0abb932109ceToomas SoomeYUI().use("datasource-get", "datasource-jsonschema", "datatable-datasource", function(Y) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome var dataSource = new Y.DataSource.IO({
199767f8919635c4928607450d9e0abb932109ceToomas Soome source: "ylocal_proxy.php?"
199767f8919635c4928607450d9e0abb932109ceToomas Soome });
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome dataSource.plug(Y.Plugin.DataSourceXMLSchema, {
199767f8919635c4928607450d9e0abb932109ceToomas Soome schema: {
199767f8919635c4928607450d9e0abb932109ceToomas Soome resultListLocator: "Result",
199767f8919635c4928607450d9e0abb932109ceToomas Soome resultFields: [
199767f8919635c4928607450d9e0abb932109ceToomas Soome {key:"Title", locator:"*[local-name() ='Title']"},
199767f8919635c4928607450d9e0abb932109ceToomas Soome {key:"Phone", locator:"*[local-name() ='Phone']"},
199767f8919635c4928607450d9e0abb932109ceToomas Soome {key:"Rating", locator:"*[local-name()='Rating']/*[local-name()='AverageRating']"}
199767f8919635c4928607450d9e0abb932109ceToomas Soome ]
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome });
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome var table = new Y.DataTable({
199767f8919635c4928607450d9e0abb932109ceToomas Soome columns: ["Title", "Phone", "Rating"],
199767f8919635c4928607450d9e0abb932109ceToomas Soome summary: "Chinese restaurants near 98089",
199767f8919635c4928607450d9e0abb932109ceToomas Soome caption: "Table with XML data from same-domain script"
199767f8919635c4928607450d9e0abb932109ceToomas Soome });
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome table.plug(Y.Plugin.DataTableDataSource, {
199767f8919635c4928607450d9e0abb932109ceToomas Soome datasource: dataSource,
199767f8919635c4928607450d9e0abb932109ceToomas Soome initialRequest: "zip=94089&query=chinese"
199767f8919635c4928607450d9e0abb932109ceToomas Soome });
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome dataSource.after("response", function() {
199767f8919635c4928607450d9e0abb932109ceToomas Soome table.render("#pizza")}
199767f8919635c4928607450d9e0abb932109ceToomas Soome );
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome // Make another request later
199767f8919635c4928607450d9e0abb932109ceToomas Soome //table.datasource.load({request:"zip=94089&query=pizza"});
199767f8919635c4928607450d9e0abb932109ceToomas Soome});
```