0

目前我正在使用Tabview addable plugin,它是由用户在单击 tabview 小部件的添加按钮时启动的。

现在我还想在某些事件上动态添加一个选项卡。

我尝试使用 Tabview 的添加方法(http://developer.yahoo.com/yui/3/api/WidgetParent.html#method_add)


function myJsFunc(name, content){

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

}

这里 tabview3 是 Tabview 小部件的全局对象。这似乎不起作用。有什么我想念的吗?任何帮助表示赞赏。

4

2 回答 2

2

以下将允许您创建两个按钮来添加和删除选项卡。

...
<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();
...
于 2011-04-27T22:24:10.987 回答
0

我在使用 YUI3 时遇到了同样的问题。一直想不通。如果我可以重新启动项目,我会选择 YUI2。关于 YUI2 的文档还有很多,更不用说 tabview 仍处于 v3 测试版中。

话虽如此,我使用以下代码解决了这个问题:

 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);
    }
});
于 2011-03-17T16:21:33.563 回答