15

ビューモデルで選択したタブを追跡しようとしていますが、うまくいかないようです。

次のコードでは、タブをクリックするとヘッダーは正しく更新されますが、タブのコンテンツは表示されません。削除する, click: $parent.selectSectionと、コンテンツは表示されますが、ヘッダーは更新されません。

data-bind="css: { active: selected }"からを削除するliと、タブをクリックすると機能するように見えますが、2 番目のタブを選択するボタンは機能しません。

どうすればこれを機能させることができますか?

参照: 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());
4

1 に答える 1

31

activeブートストラップの JS を使用するか、Knockout でクラスを追加/削除するだけで、これを処理できる方法がいくつかあります。

Knockout だけでこれを行うには、セクション自体が現在選択されているかどうかを判断するために計算される 1 つのソリューションを次に示します。

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/

于 2013-05-11T20:51:54.540 に答える