PageRenderTime 61ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/yui/docs/datatable-deprecated/datatable-dsget.html

https://bitbucket.org/mangel290/cars
HTML | 327 lines | 241 code | 86 blank | 0 comment | 0 complexity | 3681d12f14f4bc4130094d278d3104f6 MD5 | raw file
Possible License(s): Apache-2.0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Example: DataTable + DataSource.Get + JSON 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>
  11. </head>
  12. <body>
  13. <div id="doc">
  14. <h1>Example: DataTable + DataSource.Get + JSON Data</h1>
  15. <div class="yui3-g">
  16. <div class="yui3-u-3-4">
  17. <div id="main">
  18. <div class="content"><style scoped>
  19. /* custom styles for this example */
  20. .dt-example {margin:1em;}
  21. /* css to counter global site css */
  22. .dt-example th {text-transform:none;}
  23. .dt-example table {width:auto;}
  24. .dt-example caption {display:table-caption;}
  25. .notice {
  26. background: #faf3d1;
  27. border: 1px solid #eac9a9;
  28. -moz-border-radius: 3px;
  29. -webkit-border-radius: 3px;
  30. border-radius: px;
  31. padding: 0 1em;
  32. -moz-box-shadow: 0 0 5px #ccc8b3;
  33. -webkit-box-shadow: 0 0 5px #ccc8b3;
  34. box-shadow: 0 0 5px #ccc8b3;
  35. margin-bottom: 1em;
  36. }
  37. </style>
  38. <div class="notice">
  39. <p>
  40. <strong>NOTICE</strong>: This example is for the <strong>deprecated
  41. version of DataTable</strong>. The components referred to here will be
  42. removed in future versions of YUI</strong>. If you are unable to
  43. upgrade to <a href="../datatable/index.html">the latest DataTable
  44. version</a> due to unresolvable feature conflicts from version 3.4.1 or
  45. prior, please <a href="../../../projects/yui3/newticket/">file a
  46. ticket</a>.
  47. </p>
  48. </div>
  49. <div class="intro">
  50. <p>This example shows how to populate a DataTable with data from the Yahoo! Local webservice retrieved via a YQL query. First we create a DataSource.Get instance pointing to YQL, then using the DataTableDataSource plugin we can load data for pizza places near our office.</p>
  51. <p>In this example, we render the DataTable first, then load data into it in a separate call.</p>
  52. </div>
  53. <div class="example yui3-skin-sam">
  54. <div id="pizza" class="yui3-skin-sam dt-example"></div>
  55. <script>
  56. YUI().use("datasource-get", "datasource-jsonschema", "datatable-base-deprecated", "datatable-datasource-deprecated", function (Y) {
  57. var url = "http://query.yahooapis.com/v1/public/yql?format=json" +
  58. "&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys",
  59. query = "&q=" + encodeURIComponent(
  60. 'select * from local.search ' +
  61. 'where zip = "94089" and query = "pizza"'),
  62. dataSource,
  63. table;
  64. dataSource = new Y.DataSource.Get({ source: url });
  65. dataSource.plug(Y.Plugin.DataSourceJSONSchema, {
  66. schema: {
  67. resultListLocator: "query.results.Result",
  68. resultFields: [
  69. "Title",
  70. "Phone",
  71. {
  72. key: "Rating.AverageRating",
  73. // YQL is returning "NaN" for unrated restaurants
  74. parser: function (val) {
  75. return isNaN(val) ? '(none)' : val;
  76. }
  77. }
  78. ]
  79. }
  80. });
  81. table = new Y.DataTable.Base({
  82. columnset: [
  83. "Title",
  84. "Phone",
  85. { key:"Rating.AverageRating", label:"Rating" }
  86. ],
  87. summary: "Pizza places near 98089",
  88. caption: "Table with JSON data from YQL"
  89. });
  90. table.plug(Y.Plugin.DataTableDataSource, { datasource: dataSource });
  91. table.render("#pizza");
  92. table.datasource.load({ request: query });
  93. });
  94. </script>
  95. </div>
  96. <h2>Populating Your DataTable with Remote Data Using DataSource.Get</h2>
  97. <p>Your table can be easily popluated with remote JSON data from a JSONP webservice by creating a DataSource instance and using the DataTableDataSource plugin to load the data into a DataTable.</p>
  98. <p>Start with the <code>use()</code> statement:</p>
  99. <pre class="code prettyprint">YUI().use(&quot;datasource-get&quot;, &quot;datasource-jsonschema&quot;, &quot;datatable-datasource-deprecated&quot;, function(Y) {
  100. });</pre>
  101. <p>Next create a DataSource.Get instance pointing to YQL. The <a href="http://developer.yahoo.com/yql/console/">YQL Console</a> makes it easy to determine the REST URL we want to send. You also need to 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>
  102. <pre class="code prettyprint">var dataSource = new Y.DataSource.Get({
  103. source: &quot;http:&#x2F;&#x2F;query.yahooapis.com&#x2F;v1&#x2F;public&#x2F;yql?&quot;+
  104. &quot;q=select%20*%20from%20local.search%20where%20zip%3D%2794089%27%20&quot;+
  105. &quot;and%20query%3D%27pizza%27&amp;format=json&amp;&quot;+
  106. &quot;env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&quot;
  107. });
  108. dataSource.plug(Y.Plugin.DataSourceJSONSchema, {
  109. schema: {
  110. resultListLocator: &quot;query.results.Result&quot;,
  111. resultFields: [&quot;Title&quot;, &quot;Phone&quot;, &quot;Rating.AverageRating&quot;]
  112. }
  113. });
  114. dataSource.sendRequest({
  115. callback: {
  116. success: function(e) {
  117. Y.log(e);
  118. }
  119. }
  120. });</pre>
  121. <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>
  122. <pre class="code prettyprint">var cols = [
  123. &quot;Title&quot;,
  124. &quot;Phone&quot;,
  125. { key: &quot;Rating.AverageRating&quot;, label: &quot;Rating&quot; }
  126. ];</pre>
  127. <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. After you render the table, load the data via the plugin.</p>
  128. <pre class="code prettyprint">var table = new Y.DataTable.Base({
  129. columnset: cols,
  130. summary:&quot;Pizza places near 98089&quot;,
  131. caption:&quot;Table with JSON data from YQL&quot;
  132. });
  133. table.plug(Y.Plugin.DataTableDataSource, {
  134. datasource: dataSource
  135. }).render(&quot;#pizza&quot;);
  136. table.datasource.load();</pre>
  137. <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. You can see this in the <a href="#fullcode">Full Code Listing</a> below.</p>
  138. <h2 id="fullcode">Full Code Listing</h2>
  139. <pre class="code prettyprint">&lt;div id=&quot;pizza&quot; class=&quot;yui3-skin-sam dt-example&quot;&gt;&lt;&#x2F;div&gt;
  140. &lt;script&gt;
  141. YUI().use(&quot;datasource-get&quot;, &quot;datasource-jsonschema&quot;, &quot;datatable-base-deprecated&quot;, &quot;datatable-datasource-deprecated&quot;, function (Y) {
  142. var url = &quot;http:&#x2F;&#x2F;query.yahooapis.com&#x2F;v1&#x2F;public&#x2F;yql?format=json&quot; +
  143. &quot;&amp;env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&quot;,
  144. query = &quot;&amp;q=&quot; + encodeURIComponent(
  145. &#x27;select * from local.search &#x27; +
  146. &#x27;where zip = &quot;94089&quot; and query = &quot;pizza&quot;&#x27;),
  147. dataSource,
  148. table;
  149. dataSource = new Y.DataSource.Get({ source: url });
  150. dataSource.plug(Y.Plugin.DataSourceJSONSchema, {
  151. schema: {
  152. resultListLocator: &quot;query.results.Result&quot;,
  153. resultFields: [
  154. &quot;Title&quot;,
  155. &quot;Phone&quot;,
  156. {
  157. key: &quot;Rating.AverageRating&quot;,
  158. &#x2F;&#x2F; YQL is returning &quot;NaN&quot; for unrated restaurants
  159. parser: function (val) {
  160. return isNaN(val) ? &#x27;(none)&#x27; : val;
  161. }
  162. }
  163. ]
  164. }
  165. });
  166. table = new Y.DataTable.Base({
  167. columnset: [
  168. &quot;Title&quot;,
  169. &quot;Phone&quot;,
  170. { key:&quot;Rating.AverageRating&quot;, label:&quot;Rating&quot; }
  171. ],
  172. summary: &quot;Pizza places near 98089&quot;,
  173. caption: &quot;Table with JSON data from YQL&quot;
  174. });
  175. table.plug(Y.Plugin.DataTableDataSource, { datasource: dataSource });
  176. table.render(&quot;#pizza&quot;);
  177. table.datasource.load({ request: query });
  178. });
  179. &lt;&#x2F;script&gt;</pre>
  180. </div>
  181. </div>
  182. </div>
  183. <div class="yui3-u-1-4">
  184. <div class="sidebar">
  185. <div class="sidebox">
  186. <div class="hd">
  187. <h2 class="no-toc">Examples</h2>
  188. </div>
  189. <div class="bd">
  190. <ul class="examples">
  191. <li data-description="This example illustrates simple DataTable use cases.">
  192. <a href="datatable-basic.html">Basic DataTable</a>
  193. </li>
  194. <li data-description="DataTable loaded with JSON data from a remote webservice via DataSource.Get">
  195. <a href="datatable-dsget.html">DataTable + DataSource.Get + JSON Data</a>
  196. </li>
  197. <li data-description="DataTable loaded with XML data from a remote webservice via DataSource.IO.">
  198. <a href="datatable-dsio.html">DataTable + DataSource.IO + XML Data</a>
  199. </li>
  200. <li data-description="Custom format data for display.">
  201. <a href="datatable-formatting.html">Formatting Row Data for Display</a>
  202. </li>
  203. <li data-description="DataTable with nested column headers.">
  204. <a href="datatable-nestedcols.html">Nested Column Headers</a>
  205. </li>
  206. <li data-description="DataTable with column sorting.">
  207. <a href="datatable-sort.html">Column Sorting</a>
  208. </li>
  209. <li data-description="DataTable with vertical and/or horizontal scrolling rows.">
  210. <a href="datatable-scroll.html">Scrolling DataTable</a>
  211. </li>
  212. </ul>
  213. </div>
  214. </div>
  215. </div>
  216. </div>
  217. </div>
  218. </div>
  219. <script src="../assets/vendor/prettify/prettify-min.js"></script>
  220. <script>prettyPrint();</script>
  221. <script>
  222. YUI.Env.Tests = {
  223. examples: [],
  224. project: '../assets',
  225. assets: '../assets/datatable-deprecated',
  226. name: 'datatable-dsget',
  227. title: 'DataTable + DataSource.Get + JSON Data',
  228. newWindow: '',
  229. auto: false
  230. };
  231. YUI.Env.Tests.examples.push('datatable-basic');
  232. YUI.Env.Tests.examples.push('datatable-dsget');
  233. YUI.Env.Tests.examples.push('datatable-dsio');
  234. YUI.Env.Tests.examples.push('datatable-formatting');
  235. YUI.Env.Tests.examples.push('datatable-nestedcols');
  236. YUI.Env.Tests.examples.push('datatable-sort');
  237. YUI.Env.Tests.examples.push('datatable-scroll');
  238. </script>
  239. <script src="../assets/yui/test-runner.js"></script>
  240. </body>
  241. </html>