Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep track of tabs with knockoutjs + twitter bootstrap

I'm trying to keep track of the selected tab in the view model but I can't seem to make it work.

In the following code when you click a tab the header will update correctly but the content of the tab is not displayed. If you remove , click: $parent.selectSection then the contents are shown but the header does not update.

Now if you remove the data-bind="css: { active: selected }" from the li then it seems to work when you click the tabs but the button to select the second tab doesn't.

How can I make this work?

See: 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());
like image 286
Manuel Avatar asked May 11 '13 19:05

Manuel


1 Answers

There are several ways that you can handle this either using bootstrap's JS or by just having Knockout add/remove the active class.

To do this just with Knockout, here is one solution where the Section itself has a computed to determine if it is currently selected.

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());

Markup would look like:

<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>

Sample here: http://jsfiddle.net/rniemeyer/cGMTV/

There are a number of variations that you could use, but I think that this is a simple approach.

Here is a tweak where the active tab used the section name as a template: http://jsfiddle.net/rniemeyer/wbtvM/

like image 194
RP Niemeyer Avatar answered Nov 08 '22 22:11

RP Niemeyer