4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly<div class="intro">
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly <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>
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly <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>
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly <p><strong>Note:</strong> XML parsing currently has known issues on the Android WebKit browser.
819e90d415ed17d59af3a247b2ad9d6feb0c21b5Luke Smith<div class="example yui3-skin-sam">
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly{{>datatable-dsio-source}}
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly<h2>Populating Your DataTable with Remote Data using DataSource.IO</h2>
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly<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>
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly<p>Start with the `use()` statement:</p>
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny DonnellyYUI().use("datasource-io", "datasource-xmlschema", "datatable-datasource", function(Y) {
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly<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>
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnellyvar dataSource = new Y.DataSource.IO({
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly source:"ylocal_proxy.php?zip=94089&query=chinese"
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny DonnellydataSource.plug(Y.Plugin.DataSourceXMLSchema, {
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly resultListLocator: "Result",
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly resultFields: [
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly {key:"Title", locator:"*[local-name() ='Title']"},
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly {key:"Phone", locator:"*[local-name() ='Phone']"},
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly {key:"Rating", locator:"*[local-name()='Rating']/*[local-name()='AverageRating']"}
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly success: function (e) {
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly<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>
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly<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>
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnellyvar dataSource = new Y.DataSource.IO({
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly source:"ylocal_proxy.php?zip=94089&query=chinese"
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny DonnellydataSource.plug(Y.Plugin.DataSourceXMLSchema, {
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly resultListLocator: "Result",
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly resultFields: [
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly {key:"Title", locator:"*[local-name() ='Title']"},
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly {key:"Phone", locator:"*[local-name() ='Phone']"},
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly {key:"Rating", locator:"*[local-name()='Rating']/*[local-name()='AverageRating']"}
c7aeb2c8479a339ddcc01cf5973c31ddd6277b0dLuke Smithvar table = new Y.DataTable({
c7aeb2c8479a339ddcc01cf5973c31ddd6277b0dLuke Smith columns: ["Title", "Phone", "Rating"],
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly summary: "Chinese restaurants near 98089",
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly caption: "Table with XML data from same-domain script"
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly datasource: dataSource,
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly initialRequest: ""
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny DonnellydataSource.after("response", function() {
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly<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.
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly<p>Here's the code to run the whole example:</p>
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny DonnellyYUI().use("datasource-get", "datasource-jsonschema", "datatable-datasource", function(Y) {
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly var dataSource = new Y.DataSource.IO({
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly dataSource.plug(Y.Plugin.DataSourceXMLSchema, {
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly resultListLocator: "Result",
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly resultFields: [
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly {key:"Title", locator:"*[local-name() ='Title']"},
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly {key:"Phone", locator:"*[local-name() ='Phone']"},
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly {key:"Rating", locator:"*[local-name()='Rating']/*[local-name()='AverageRating']"}
c7aeb2c8479a339ddcc01cf5973c31ddd6277b0dLuke Smith var table = new Y.DataTable({
c7aeb2c8479a339ddcc01cf5973c31ddd6277b0dLuke Smith columns: ["Title", "Phone", "Rating"],
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly summary: "Chinese restaurants near 98089",
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly caption: "Table with XML data from same-domain script"
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly datasource: dataSource,
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly initialRequest: "zip=94089&query=chinese"
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly dataSource.after("response", function() {
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly // Make another request later
4c7c8763e6b273c36fef74ccede731c4b7a679f3Jenny Donnelly //table.datasource.load({request:"zip=94089&query=pizza"});