Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knockout.js bind bootstrap btn-group

I'm wondering if it's possible to bind the selected value in a Twitter bootstrap radio btn-group to an observable in knockout.js?

In my case, I'm binding the group to an observableArray like so

  <div class="btn-group" data-toggle="buttons-radio" data-bind="foreach: myItemTypes">
    <button class="btn active" data-bind="html: Value, Id: Key">${ Description }</button>
  </div>

Any guidance?

Cheers,

Fergal.

like image 204
Fergal Moran Avatar asked Mar 11 '12 15:03

Fergal Moran


2 Answers

Here is an example of how this could be done:

http://jsfiddle.net/jearles/vu8pk/

By using the css binding we can dynamically turn the "active" class on/off based on the selected observable for each button.

like image 123
John Earles Avatar answered Oct 17 '22 11:10

John Earles


See also checkedButtons bindingHandler

like image 7
cuhuak Avatar answered Oct 17 '22 11:10

cuhuak