javascript - YUI 选项卡 View : "add tab" button stuck on the left side when all tabs are closed

标签 javascript yui yui3 tabview

我正在使用 YUI TabView 小部件来添加和删除选项卡,如 yuilibrary-tabview-add-remove 中所述。 .

我注意到一个“错误”或者可能只是缺少一个功能:当您关闭所有选项卡然后添加新选项卡时,“添加选项卡”按钮将会卡在标签栏的左侧,并且所有新标签都将在右侧排序。如果您不关闭所有选项卡,则该按钮无论如何都会始终位于右侧。

现在,我添加了一个解决方法:添加新选项卡时,将检测无选项卡状态,并且将使用 jQuery 对 DOM li-item 进行排序强> after() 方法。最后,新添加的选项卡将被选中:

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

  // When previously no tabs present, move 'add button' to end after adding a new tab
  if ( tabview.size() == 1) {
    var addTabButton = $('#addTabButton');
    addTabButton.next().after(addTabButton);
    tabview.selectChild(0);
  };
}

但是,我对这个解决方案并不满意。是否有更优雅的方法来解决这个问题?

最佳答案

您的解决方案绝对有效。我只是使用 YUI 编写它,因为加载 YUI 和 jQuery 在 kweight 和维护成本方面非常昂贵(您和您的同事需要掌握两个库)。

一个干净的选择是在初始化程序中创建一个节点并保留对其的引用,以便您以后可以移动它:

initializer: function (config) {
  var tabview = this.get('host');

  // create the node before rendering and keep a reference to it
  this._addNode = Y.Node.create(this.ADD_TEMPLATE);

  tabview.after('render', this.afterRender, this);

  tabview.get('contentBox')
    .delegate('click', this.onAddClick, '.yui3-tab-add', this);
},

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

afterRender: function (e) {
  this._appendAddNode();
},

onAddClick: function (e) {
  e.stopPropagation();

  var tabview = this.get('host'), input = this.getTabInput();
  tabview.add(input, input.index);

  // When previously no tabs present, move 'add button' to end after adding a new tab
  if ( tabview.size() == 1) {
    // _addNode will already be present, but by using append() it'll be moved to the
    // last place in the list
    this._appendAddNode();
  };
}

这是一个工作版本:http://jsbin.com/iLiM/2/

关于javascript - YUI 选项卡 View : "add tab" button stuck on the left side when all tabs are closed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18275026/

相关文章:

liferay - AlloyUI "Modal"组件不是真正的模态

javascript - Mouseenter 无法在带有 YUI 的 chrome 中工作

javascript - 具有多个系列的 YUI 图表

javascript - 如何使用 YUI3 按字母顺序对列表进行排序

javascript - Chart.js 修复最小轴?

javascript - 查找集合中是否有元素具有 style.display !== "none";

javascript - 如何将添加到列表框客户端的值存储到 sessionScope

javascript - YUI3 - 创建全局变量以避免使用 .use(...);

css - 你将类/id 保留在同一个 div 上还是将它们分开?

javascript - 我的代码在 IE8 中真的很慢。但在 Safari、firefox、chrome 中效果很好