src/cm/media/js/lib/yui/yui_3.10.3/docs/datatable/datatable-dsio.html
author Yves-Marie Haussonne <ymh.work+github@gmail.com>
Fri, 09 May 2014 18:35:26 +0200
changeset 656 a84519031134
parent 525 89ef5ed3c48b
permissions -rw-r--r--
add link to "privacy policy" in the header test

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: DataTable + DataSource.IO + XML Data</title>
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic">
    <link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <link rel="shortcut icon" type="image/png" href="../assets/favicon.png">
    <script src="../../build/yui/yui-min.js"></script>
    
</head>
<body>
<!--
<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>
-->
<div id="doc">
    <div id="hd">
        <h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1>
    </div>
    

            <h1>Example: DataTable + DataSource.IO + XML Data</h1>
    <div class="yui3-g">
        <div class="yui3-u-3-4">
            <div id="main">
                <div class="content"><div class="intro">
    <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>

    <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>

    <p><strong>Note:</strong> XML parsing currently has known issues on the Android WebKit browser.
</div>

<div class="example yui3-skin-sam">
<style scoped>
/* custom styles for this example */
.example .yui3-datatable {
    margin-bottom: 1em;
}

/* css to counter global site css */
.example table {
    width: auto;
}
.example caption {
    display: table-caption;
}
.example th,
.example td {
    text-transform: none;
    border: 0 none;
}
</style>

<div id="chinese" class="dt-example"></div>

<script>
YUI().use("datasource-io", "datasource-xmlschema", "datatable-datasource", function (Y) {
    var ds = new Y.DataSource.IO({
        source:"../assets/datatable/ylocal.xml?"})
        .plug(Y.Plugin.DataSourceXMLSchema, {
            schema: {
                resultListLocator: "Result",
                resultFields: [
                    {
                        key:"Title",
                        locator:"*[local-name() ='Title']"
                    },
                    {
                        key:"Phone",
                        locator:"*[local-name() ='Phone']"
                    },
                    {
                        key:"Rating",
                        locator:"*[local-name()='Rating']/*[local-name()='AverageRating']",
                        // The data contains "NaN" for unrated restaurants
                        parser: function (val) {
                            return isNaN(val) ? '(none)' : val;
                        }
                    }
                ]
            }
        }),
    dt = new Y.DataTable({
        columns: ["Title", "Phone", "Rating"],
        summary:"Chinese restaurants near 98089",
        caption:"Table with XML data from same-domain script"
    }).plug(Y.Plugin.DataTableDataSource, {
        datasource: ds,
        initialRequest:"zip=94089&query=chinese"
    }).render("#chinese");

    dt.datasource.load();
});
</script>

</div>

<h2>Populating Your DataTable with Remote Data using DataSource.IO</h2>
<p>
<strong>Note:</strong> be sure to add the <code>yui3-skin-sam</code> classname to the
page's <code>&lt;body&gt;</code> element or to a parent element of the widget in order to apply
the default CSS skin. See <a href="http://yuilibrary.com/yui/docs/tutorials/skins/">Understanding Skinning</a>.
</p>
<pre class="code prettyprint">&lt;body class=&quot;yui3-skin-sam&quot;&gt; &lt;!-- You need this skin class --&gt;</pre>

<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>

<p>Start with the <code>use()</code> statement:</p>

<pre class="code prettyprint">YUI().use(&quot;datasource-io&quot;, &quot;datasource-xmlschema&quot;, &quot;datatable-datasource&quot;, function(Y) {
});</pre>


<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>

<pre class="code prettyprint">var dataSource = new Y.DataSource.IO({
    source:&quot;ylocal_proxy.php?zip=94089&amp;query=chinese&quot;
});

dataSource.plug(Y.Plugin.DataSourceXMLSchema, {
    schema: {
        resultListLocator: &quot;Result&quot;,
        resultFields: [
            {key:&quot;Title&quot;, locator:&quot;*[local-name() =&#x27;Title&#x27;]&quot;},
            {key:&quot;Phone&quot;, locator:&quot;*[local-name() =&#x27;Phone&#x27;]&quot;},
            {key:&quot;Rating&quot;, locator:&quot;*[local-name()=&#x27;Rating&#x27;]&#x2F;*[local-name()=&#x27;AverageRating&#x27;]&quot;}
        ]
    }
});

dataSource.sendRequest({    
    callback: {
        success: function (e) {
            Y.log(e);
        }
    }
});</pre>


<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>

<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>

<pre class="code prettyprint">var dataSource = new Y.DataSource.IO({
    source:&quot;ylocal_proxy.php?zip=94089&amp;query=chinese&quot;
});

dataSource.plug(Y.Plugin.DataSourceXMLSchema, {
    schema: {
        resultListLocator: &quot;Result&quot;,
        resultFields: [
            {key:&quot;Title&quot;, locator:&quot;*[local-name() =&#x27;Title&#x27;]&quot;},
            {key:&quot;Phone&quot;, locator:&quot;*[local-name() =&#x27;Phone&#x27;]&quot;},
            {key:&quot;Rating&quot;, locator:&quot;*[local-name()=&#x27;Rating&#x27;]&#x2F;*[local-name()=&#x27;AverageRating&#x27;]&quot;}
        ]
    }
});

var table = new Y.DataTable({
    columns: [&quot;Title&quot;, &quot;Phone&quot;, &quot;Rating&quot;],
    summary: &quot;Chinese restaurants near 98089&quot;,
    caption: &quot;Table with XML data from same-domain script&quot;
});
table.plug(Y.Plugin.DataTableDataSource, {
    datasource: dataSource,
    initialRequest: &quot;&quot;
});

dataSource.after(&quot;response&quot;, function() {
    table.render(&quot;#pizza&quot;)}
);</pre>


<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.

<p>Here's the code to run the whole example:</p>

<pre class="code prettyprint">YUI().use(&quot;datasource-get&quot;, &quot;datasource-jsonschema&quot;, &quot;datatable-datasource&quot;, function(Y) {
    var dataSource = new Y.DataSource.IO({
        source: &quot;ylocal_proxy.php?&quot;
    });

    dataSource.plug(Y.Plugin.DataSourceXMLSchema, {
        schema: {
            resultListLocator: &quot;Result&quot;,
            resultFields: [
                {key:&quot;Title&quot;, locator:&quot;*[local-name() =&#x27;Title&#x27;]&quot;},
                {key:&quot;Phone&quot;, locator:&quot;*[local-name() =&#x27;Phone&#x27;]&quot;},
                {key:&quot;Rating&quot;, locator:&quot;*[local-name()=&#x27;Rating&#x27;]&#x2F;*[local-name()=&#x27;AverageRating&#x27;]&quot;}
            ]
        }
    });

    var table = new Y.DataTable({
        columns: [&quot;Title&quot;, &quot;Phone&quot;, &quot;Rating&quot;],
        summary: &quot;Chinese restaurants near 98089&quot;,
        caption: &quot;Table with XML data from same-domain script&quot;
    });
    
    table.plug(Y.Plugin.DataTableDataSource, {
        datasource: dataSource,
        initialRequest: &quot;zip=94089&amp;query=chinese&quot;
    });

    dataSource.after(&quot;response&quot;, function() {
        table.render(&quot;#pizza&quot;)}
    );

    &#x2F;&#x2F; Make another request later
    &#x2F;&#x2F;table.datasource.load({request:&quot;zip=94089&amp;query=pizza&quot;});
});</pre>

</div>
            </div>
        </div>

        <div class="yui3-u-1-4">
            <div class="sidebar">
                

                
                    <div class="sidebox">
                        <div class="hd">
                            <h2 class="no-toc">Examples</h2>
                        </div>

                        <div class="bd">
                            <ul class="examples">
                                
                                    
                                        <li data-description="This example illustrates simple DataTable use cases.">
                                            <a href="datatable-basic.html">Basic DataTable</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="DataTable loaded with JSON data from a remote webservice via DataSource.Get">
                                            <a href="datatable-dsget.html">DataTable + DataSource.Get + JSON Data</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="DataTable loaded with XML data from a remote webservice via DataSource.IO.">
                                            <a href="datatable-dsio.html">DataTable + DataSource.IO + XML Data</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="Custom format data for display.">
                                            <a href="datatable-formatting.html">Formatting Row Data for Display</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="DataTable with nested column headers.">
                                            <a href="datatable-nestedcols.html">Nested Column Headers</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="DataTable with column sorting.">
                                            <a href="datatable-sort.html">Column Sorting</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="DataTable with vertical and/or horizontal scrolling rows.">
                                            <a href="datatable-scroll.html">Scrolling DataTable</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="Using DataTable&#x27;s recordType attribute to create calculated, sortable columns.">
                                            <a href="datatable-recordtype.html">Sortable generated columns</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="Populating one DataTable from details in the data of another.">
                                            <a href="datatable-masterdetail.html">Master and detail tables</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="Checkbox column that retains checked state when sorting.">
                                            <a href="datatable-chkboxselect.html">Checkbox select column</a>
                                        </li>
                                    
                                
                                    
                                
                            </ul>
                        </div>
                    </div>
                

                
                    <div class="sidebox">
                        <div class="hd">
                            <h2 class="no-toc">Examples That Use This Component</h2>
                        </div>

                        <div class="bd">
                            <ul class="examples">
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                
                                    
                                        <li data-description="Shows how to instantiate multiple Panel instances, and use nested modality to interact with a Datatable.">
                                            <a href="../panel/panel-form.html">Creating a Modal Form</a>
                                        </li>
                                    
                                
                            </ul>
                        </div>
                    </div>
                
            </div>
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

<script>
YUI.Env.Tests = {
    examples: [],
    project: '../assets',
    assets: '../assets/datatable',
    name: 'datatable-dsio',
    title: 'DataTable + DataSource.IO + XML Data',
    newWindow: '',
    auto:  false 
};
YUI.Env.Tests.examples.push('datatable-basic');
YUI.Env.Tests.examples.push('datatable-dsget');
YUI.Env.Tests.examples.push('datatable-dsio');
YUI.Env.Tests.examples.push('datatable-formatting');
YUI.Env.Tests.examples.push('datatable-nestedcols');
YUI.Env.Tests.examples.push('datatable-sort');
YUI.Env.Tests.examples.push('datatable-scroll');
YUI.Env.Tests.examples.push('datatable-recordtype');
YUI.Env.Tests.examples.push('datatable-masterdetail');
YUI.Env.Tests.examples.push('datatable-chkboxselect');
YUI.Env.Tests.examples.push('panel-form');

</script>
<script src="../assets/yui/test-runner.js"></script>



</body>
</html>