src/cm/media/js/lib/yui/yui_3.10.3/docs/tabview/tabview-fromjs.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: Dynamic TabView from JavaScript</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: Dynamic TabView from JavaScript</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 create a <code>TabView</code> widget dynamically and insert it into the page.</p>
</div>

<div class="example yui3-skin-sam">
<div id="demo">
</div>

<script type="text/javascript">
YUI().use('tabview', function(Y) {
    var tabview = new Y.TabView({
        children: [{
            label: 'foo',
            content: '<p>foo content</p>'
        }, {
            label: 'bar',
            content: '<p>bar content</p>'
        }, {
            label: 'baz',
            content: '<p>baz content</p>'
        }]
    });

    tabview.render('#demo');
    tabview.selectChild(2);
});
</script>

</div>

<h2>Creating a TabView From JavaScript</h2>

<p>A <code>TabView</code> can be created dynamically using a small amount of JavaScript.</p>

<h3>The Markup</h3>

<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>There are no other markup requirements in this case, although you may want to have a placeholder to render the tabview into, which is what this example does.</p>

<pre class="code prettyprint">&lt;div id=&quot;demo&quot;&gt;
&lt;&#x2F;div&gt;</pre>


<h3>The JavaScript</h3>

<p>The minimal requirement for creating a <code>TabView</code> from scratch are the labels and content for each tab.
These are added as the <code>children</code> attribute when creating the <code>TabView</code>.</p>

<pre class="code prettyprint">var tabview = new Y.TabView({
    children: [{
        label: &#x27;foo&#x27;,
        content: &#x27;&lt;p&gt;foo content&lt;&#x2F;p&gt;&#x27;
    }, {
        label: &#x27;bar&#x27;,
        content: &#x27;&lt;p&gt;bar content&lt;&#x2F;p&gt;&#x27;
    }, {
        label: &#x27;baz&#x27;,
        content: &#x27;&lt;p&gt;baz content&lt;&#x2F;p&gt;&#x27;
    }]
});</pre>


<h3>Rendering</h3>

<p>Calling render creates the <code>TabView</code>, inserting into the node
passed to render.
</p>

<pre class="code prettyprint">tabview.render(&#x27;#demo&#x27;);</pre>


<h3>Changing the Selected Tab</h3>

<p>By default, clicking a tab makes it selected and shows its content.  You can also do this programmatically
by calling the <code>selectChild</code> method on the <code>TabView</code>, passing it the index of the
tab to be selected.
</p>

<pre class="code prettyprint">tabview.selectChild(2);</pre>


<h2>Complete Example Source</h2>
<pre class="code prettyprint">&lt;div id=&quot;demo&quot;&gt;
&lt;&#x2F;div&gt;

&lt;script type=&quot;text&#x2F;javascript&quot;&gt;
YUI().use(&#x27;tabview&#x27;, function(Y) {
    var tabview = new Y.TabView({
        children: [{
            label: &#x27;foo&#x27;,
            content: &#x27;&lt;p&gt;foo content&lt;&#x2F;p&gt;&#x27;
        }, {
            label: &#x27;bar&#x27;,
            content: &#x27;&lt;p&gt;bar content&lt;&#x2F;p&gt;&#x27;
        }, {
            label: &#x27;baz&#x27;,
            content: &#x27;&lt;p&gt;baz content&lt;&#x2F;p&gt;&#x27;
        }]
    });

    tabview.render(&#x27;#demo&#x27;);
    tabview.selectChild(2);
});
&lt;&#x2F;script&gt;</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 shows how to create a TabView wigdet from existing HTML.">
                                            <a href="tabview-basic.html">TabView from Existing Markup</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="This example shows how to create a TabView wigdet from JavaScript.">
                                            <a href="tabview-fromjs.html">Dynamic TabView from JavaScript</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="This example shows how to add and remove Tabs.">
                                            <a href="tabview-add-remove.html">Adding and Removing Tabs</a>
                                        </li>
                                    
                                
                                    
                                        <li data-description="This example shows how to load tab content remotely using a YQL plugin.">
                                            <a href="tabview-yql.html">Loading Tab Content</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="Demonstrates how to add browser history support to a TabView widget using the History Utility.">
                                            <a href="../history/history-tabview.html">History + TabView</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/tabview',
    name: 'tabview-fromjs',
    title: 'Dynamic TabView from JavaScript',
    newWindow: '',
    auto:  false 
};
YUI.Env.Tests.examples.push('tabview-basic');
YUI.Env.Tests.examples.push('tabview-fromjs');
YUI.Env.Tests.examples.push('tabview-add-remove');
YUI.Env.Tests.examples.push('tabview-yql');
YUI.Env.Tests.examples.push('history-tabview');

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



</body>
</html>