使用knockoutjs + twitter bootstrap跟踪标签

时间:2013-05-11 19:58:54

标签: javascript html twitter-bootstrap knockout.js

我正在尝试跟踪视图模型中的选定选项卡,但我似乎无法使其正常工作。

在以下代码中单击选项卡时,标题将正确更新,但不会显示选项卡的内容。如果您删除, click: $parent.selectSection,则会显示内容,但标题不会更新。

现在,如果您从data-bind="css: { active: selected }"中移除li,那么当您点击标签时它似乎有效,但选择第二个标签的按钮却没有。

我该如何做到这一点?

请参阅:http://jsfiddle.net/5PgE2/3/

HTML:

<h3>
    <span>Selected: </span>
    <span data-bind="text: selectedSection().name" />
</h3>
<div class="tabbable">
    <ul class="nav nav-tabs" data-bind="foreach: sections">
        <li data-bind="css: { active: selected }">
            <a data-bind="attr: { href: '#tab' + name }
, click: $parent.selectSection" data-toggle="tab">
                <span data-bind="text: name" />
            </a>
        </li>
    </ul>
    <div class="tab-content" data-bind="foreach: sections">
        <div class="tab-pane" data-bind="attr: { id: 'tab' + name }">
            <span data-bind="text: 'In section: ' + name" />
        </div>
    </div>
</div>
<button data-bind="click: selectTwo">Select tab Two</button>

JS:

var Section = function (name) {
    this.name = name;
    this.selected = ko.observable(false);
}

var ViewModel = function () {
    var self = this;
    self.sections = ko.observableArray([new Section('One'),
    new Section('Two'),
    new Section('Three')]);
    self.selectedSection = ko.observable(new Section(''));
    self.selectSection = function (s) {
        self.selectedSection().selected(false);

        self.selectedSection(s);
        self.selectedSection().selected(true);
    }

    self.selectTwo = function() { self.selectSection(self.sections()[1]); }
}

ko.applyBindings(new ViewModel());

1 个答案:

答案 0 :(得分:30)

有几种方法可以使用bootstrap的JS或只是让Knockout添加/删除active类来处理这个问题。

要使用Knockout执行此操作,这里有一个解决方案,其中Section本身具有计算以确定它当前是否被选中。

var Section = function (name, selected) {
    this.name = name;
    this.isSelected = ko.computed(function() {
       return this === selected();  
    }, this);
}

var ViewModel = function () {
    var self = this;

    self.selectedSection = ko.observable();

    self.sections = ko.observableArray([
        new Section('One', self.selectedSection),
        new Section('Two', self.selectedSection),
        new Section('Three', self.selectedSection)
    ]);

    //inialize to the first section
    self.selectedSection(self.sections()[0]);
}

ko.applyBindings(new ViewModel());

标记看起来像:

<div class="tabbable">
    <ul class="nav nav-tabs" data-bind="foreach: sections">
        <li data-bind="css: { active: isSelected }">
            <a href="#" data-bind="click: $parent.selectedSection">
                <span data-bind="text: name" />
            </a>
        </li>
    </ul>
    <div class="tab-content" data-bind="foreach: sections">
        <div class="tab-pane" data-bind="css: { active: isSelected }">
            <span data-bind="text: 'In section: ' + name" />
        </div>
    </div>
</div>

此处示例:http://jsfiddle.net/rniemeyer/cGMTV/

您可以使用多种变体,但我认为这是一种简单的方法。

这是一个调整,其中活动标签使用部分名称作为模板:http://jsfiddle.net/rniemeyer/wbtvM/