~bac/juju-gui/trunkcopy

« back to all changes in this revision

Viewing changes to lib/yui/docs/datatable/datatable-dsio.html

  • Committer: kapil.foss at gmail
  • Date: 2012-07-13 18:45:59 UTC
  • Revision ID: kapil.foss@gmail.com-20120713184559-2xl7be17egsrz0c9
reshape

Show diffs side-by-side

added added

removed removed

Lines of Context:
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=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
 
 
14
 
<div id="doc">
15
 
    <h1>Example: DataTable + DataSource.IO + XML Data</h1>
16
 
 
17
 
    
18
 
 
19
 
    <div class="yui3-g">
20
 
        <div class="yui3-u-3-4">
21
 
            <div id="main">
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>
24
 
 
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>
26
 
 
27
 
    <p><strong>Note:</strong> XML parsing currently has known issues on the Android WebKit browser.
28
 
</div>
29
 
 
30
 
<div class="example yui3-skin-sam">
31
 
<style scoped>
32
 
/* custom styles for this example */
33
 
.example .yui3-datatable {
34
 
    margin-bottom: 1em;
35
 
}
36
 
 
37
 
/* css to counter global site css */
38
 
.example table {
39
 
    width: auto;
40
 
}
41
 
.example caption {
42
 
    display: table-caption;
43
 
}
44
 
.example th,
45
 
.example td {
46
 
    text-transform: none;
47
 
    border: 0 none;
48
 
}
49
 
</style>
50
 
 
51
 
<div id="chinese" class="dt-example"></div>
52
 
 
53
 
<script>
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, {
58
 
            schema: {
59
 
                resultListLocator: "Result",
60
 
                resultFields: [
61
 
                    {
62
 
                        key:"Title",
63
 
                        locator:"*[local-name() ='Title']"
64
 
                    },
65
 
                    {
66
 
                        key:"Phone",
67
 
                        locator:"*[local-name() ='Phone']"
68
 
                    },
69
 
                    {
70
 
                        key:"Rating",
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;
75
 
                        }
76
 
                    }
77
 
                ]
78
 
            }
79
 
        }),
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, {
85
 
        datasource: ds,
86
 
        initialRequest:"zip=94089&query=chinese"
87
 
    }).render("#chinese");
88
 
 
89
 
    dt.datasource.load();
90
 
});
91
 
</script>
92
 
 
93
 
</div>
94
 
 
95
 
<h2>Populating Your DataTable with Remote Data using DataSource.IO</h2>
96
 
 
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>
98
 
 
99
 
<p>Start with the <code>use()</code> statement:</p>
100
 
 
101
 
<pre class="code prettyprint">YUI().use(&quot;datasource-io&quot;, &quot;datasource-xmlschema&quot;, &quot;datatable-datasource&quot;, function(Y) {
102
 
});</pre>
103
 
 
104
 
 
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>
106
 
 
107
 
<pre class="code prettyprint">var dataSource = new Y.DataSource.IO({
108
 
    source:&quot;ylocal_proxy.php?zip=94089&amp;query=chinese&quot;
109
 
});
110
 
 
111
 
dataSource.plug(Y.Plugin.DataSourceXMLSchema, {
112
 
    schema: {
113
 
        resultListLocator: &quot;Result&quot;,
114
 
        resultFields: [
115
 
            {key:&quot;Title&quot;, locator:&quot;*[local-name() =&#x27;Title&#x27;]&quot;},
116
 
            {key:&quot;Phone&quot;, locator:&quot;*[local-name() =&#x27;Phone&#x27;]&quot;},
117
 
            {key:&quot;Rating&quot;, locator:&quot;*[local-name()=&#x27;Rating&#x27;]&#x2F;*[local-name()=&#x27;AverageRating&#x27;]&quot;}
118
 
        ]
119
 
    }
120
 
});
121
 
 
122
 
dataSource.sendRequest({    
123
 
    callback: {
124
 
        success: function (e) {
125
 
            Y.log(e);
126
 
        }
127
 
    }
128
 
});</pre>
129
 
 
130
 
 
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>
132
 
 
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>
134
 
 
135
 
<pre class="code prettyprint">var dataSource = new Y.DataSource.IO({
136
 
    source:&quot;ylocal_proxy.php?zip=94089&amp;query=chinese&quot;
137
 
});
138
 
 
139
 
dataSource.plug(Y.Plugin.DataSourceXMLSchema, {
140
 
    schema: {
141
 
        resultListLocator: &quot;Result&quot;,
142
 
        resultFields: [
143
 
            {key:&quot;Title&quot;, locator:&quot;*[local-name() =&#x27;Title&#x27;]&quot;},
144
 
            {key:&quot;Phone&quot;, locator:&quot;*[local-name() =&#x27;Phone&#x27;]&quot;},
145
 
            {key:&quot;Rating&quot;, locator:&quot;*[local-name()=&#x27;Rating&#x27;]&#x2F;*[local-name()=&#x27;AverageRating&#x27;]&quot;}
146
 
        ]
147
 
    }
148
 
});
149
 
 
150
 
var table = new Y.DataTable({
151
 
    columns: [&quot;Title&quot;, &quot;Phone&quot;, &quot;Rating&quot;],
152
 
    summary: &quot;Chinese restaurants near 98089&quot;,
153
 
    caption: &quot;Table with XML data from same-domain script&quot;
154
 
});
155
 
table.plug(Y.Plugin.DataTableDataSource, {
156
 
    datasource: dataSource,
157
 
    initialRequest: &quot;&quot;
158
 
});
159
 
 
160
 
dataSource.after(&quot;response&quot;, function() {
161
 
    table.render(&quot;#pizza&quot;)}
162
 
);</pre>
163
 
 
164
 
 
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.
166
 
 
167
 
<p>Here's the code to run the whole example:</p>
168
 
 
169
 
<pre class="code prettyprint">YUI().use(&quot;datasource-get&quot;, &quot;datasource-jsonschema&quot;, &quot;datatable-datasource&quot;, function(Y) {
170
 
    var dataSource = new Y.DataSource.IO({
171
 
        source: &quot;ylocal_proxy.php?&quot;
172
 
    });
173
 
 
174
 
    dataSource.plug(Y.Plugin.DataSourceXMLSchema, {
175
 
        schema: {
176
 
            resultListLocator: &quot;Result&quot;,
177
 
            resultFields: [
178
 
                {key:&quot;Title&quot;, locator:&quot;*[local-name() =&#x27;Title&#x27;]&quot;},
179
 
                {key:&quot;Phone&quot;, locator:&quot;*[local-name() =&#x27;Phone&#x27;]&quot;},
180
 
                {key:&quot;Rating&quot;, locator:&quot;*[local-name()=&#x27;Rating&#x27;]&#x2F;*[local-name()=&#x27;AverageRating&#x27;]&quot;}
181
 
            ]
182
 
        }
183
 
    });
184
 
 
185
 
    var table = new Y.DataTable({
186
 
        columns: [&quot;Title&quot;, &quot;Phone&quot;, &quot;Rating&quot;],
187
 
        summary: &quot;Chinese restaurants near 98089&quot;,
188
 
        caption: &quot;Table with XML data from same-domain script&quot;
189
 
    });
190
 
    
191
 
    table.plug(Y.Plugin.DataTableDataSource, {
192
 
        datasource: dataSource,
193
 
        initialRequest: &quot;zip=94089&amp;query=chinese&quot;
194
 
    });
195
 
 
196
 
    dataSource.after(&quot;response&quot;, function() {
197
 
        table.render(&quot;#pizza&quot;)}
198
 
    );
199
 
 
200
 
    &#x2F;&#x2F; Make another request later
201
 
    &#x2F;&#x2F;table.datasource.load({request:&quot;zip=94089&amp;query=pizza&quot;});
202
 
});</pre>
203
 
 
204
 
</div>
205
 
            </div>
206
 
        </div>
207
 
 
208
 
        <div class="yui3-u-1-4">
209
 
            <div class="sidebar">
210
 
                
211
 
 
212
 
                
213
 
                    <div class="sidebox">
214
 
                        <div class="hd">
215
 
                            <h2 class="no-toc">Examples</h2>
216
 
                        </div>
217
 
 
218
 
                        <div class="bd">
219
 
                            <ul class="examples">
220
 
                                
221
 
                                    
222
 
                                        <li data-description="This example illustrates simple DataTable use cases.">
223
 
                                            <a href="datatable-basic.html">Basic DataTable</a>
224
 
                                        </li>
225
 
                                    
226
 
                                
227
 
                                    
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>
230
 
                                        </li>
231
 
                                    
232
 
                                
233
 
                                    
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>
236
 
                                        </li>
237
 
                                    
238
 
                                
239
 
                                    
240
 
                                        <li data-description="Custom format data for display.">
241
 
                                            <a href="datatable-formatting.html">Formatting Row Data for Display</a>
242
 
                                        </li>
243
 
                                    
244
 
                                
245
 
                                    
246
 
                                        <li data-description="DataTable with nested column headers.">
247
 
                                            <a href="datatable-nestedcols.html">Nested Column Headers</a>
248
 
                                        </li>
249
 
                                    
250
 
                                
251
 
                                    
252
 
                                        <li data-description="DataTable with column sorting.">
253
 
                                            <a href="datatable-sort.html">Column Sorting</a>
254
 
                                        </li>
255
 
                                    
256
 
                                
257
 
                                    
258
 
                                        <li data-description="DataTable with vertical and/or horizontal scrolling rows.">
259
 
                                            <a href="datatable-scroll.html">Scrolling DataTable</a>
260
 
                                        </li>
261
 
                                    
262
 
                                
263
 
                                    
264
 
                                        <li data-description="Using DataTable&#x27;s recordType attribute to create calculated, sortable columns.">
265
 
                                            <a href="datatable-recordtype.html">Sortable generated columns</a>
266
 
                                        </li>
267
 
                                    
268
 
                                
269
 
                                    
270
 
                                
271
 
                            </ul>
272
 
                        </div>
273
 
                    </div>
274
 
                
275
 
 
276
 
                
277
 
                    <div class="sidebox">
278
 
                        <div class="hd">
279
 
                            <h2 class="no-toc">Examples That Use This Component</h2>
280
 
                        </div>
281
 
 
282
 
                        <div class="bd">
283
 
                            <ul class="examples">
284
 
                                
285
 
                                    
286
 
                                
287
 
                                    
288
 
                                
289
 
                                    
290
 
                                
291
 
                                    
292
 
                                
293
 
                                    
294
 
                                
295
 
                                    
296
 
                                
297
 
                                    
298
 
                                
299
 
                                    
300
 
                                
301
 
                                    
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>
304
 
                                        </li>
305
 
                                    
306
 
                                
307
 
                            </ul>
308
 
                        </div>
309
 
                    </div>
310
 
                
311
 
            </div>
312
 
        </div>
313
 
    </div>
314
 
</div>
315
 
 
316
 
<script src="../assets/vendor/prettify/prettify-min.js"></script>
317
 
<script>prettyPrint();</script>
318
 
 
319
 
</body>
320
 
</html>