开发者

How to add a new tab in YUI3 Tabview widget without the help of Addable plugin

Currently I am using Tabview addable plugin, which is initiated by user on clicking add button of tabview widget.

Now I开发者_如何学C also want to add a tab on some event dynamically.

I tried following using add method (http://developer.yahoo.com/yui/3/api/WidgetParent.html#method_add) of Tabview


function myJsFunc(name, content){

tabview3.add({
label: name,
content: '<textarea> </textarea>',
index: Number(tabview3.size())});

}

Here tabview3 is a global object of Tabview widget. This does not seem to work. Is there any thing which I am missing ? Any help is appreciated.


Following will allow you to create two buttons to add and remove tabs.

...
<input type="button" id="addTabButton" value="Add Tab" />

<input type="button" id="removeTabButton" value="Remove Tab" />

...
var addTab = function(e) {
    e.preventDefault();
input = {"label":"test","content":"TestContent","index":"2"};
    mainTabView.add(input, input.index);
}

Y.on("click", addTab, ["#addTabButton"]);

var removeTab = function(e) {
    e.preventDefault();
    mainTabView.remove(1);
}

Y.on("click", removeTab, ["#removeTabButton"]);

var mainTabView = new Y.TabView({
srcNode:'#TheMainTabView',
    plugins: [Addable, Removeable]
});
mainTabView.render();
...


I ran into the same problem with YUI3. Never could figure it out. If I could restart the project I would go with YUI2. There is much more documentation on YUI2 not to mention tabview is still in beta for v3.

That being said, I worked around it using the following code:

 var Addable = function(config) {
    Addable.superclass.constructor.apply(this, arguments);
};

Addable.NAME = 'addableTabs';
Addable.NS = 'addable';

Y.extend(Addable, Y.Plugin.Base, {
    ADD_TEMPLATE: '<li class="yui3-tab" title="Add Your Own Custom Tab!">' +
                '<a class="yui3-tab-label yui3-tab-add">+</a></li>',

    initializer: function(config) {
        var tabview = this.get('host');
        tabview.after('render', this.afterRender, this);
        tabview.get('contentBox')
            .delegate('click', this.onAddClick, '.yui3-tab-add', this);
    },

    getTabInput: function() {
        var tabview = this.get('host');
        return {
            label: window.prompt('What do you want to call this tab?:', 'foo'),
            content: window.prompt('Page Url. Replace foo.com with whatever page you want loaded.', '<iframe src="http://www.foo.com" width="100%" height="850px" frameborder="0"></iframe>'),
            index: Number(window.prompt('What tab number would you like this to be (0 = first):', tabview.size()))
        }
    },



    afterRender: function(e) {
        var tabview = this.get('host');
        tabview.get('contentBox').one('> ul').append(this.ADD_TEMPLATE);
    },

    onAddClick: function(e) {
        e.stopPropagation();
        var tabview = this.get('host'),
            input = this.getTabInput();
        tabview.add(input, input.index);
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜