I'm attempting to use bootstrap's nicely styled button dropdowns with knockout. Unfortunately the dropdowns are built using links rather than <select>
and knockout-bootstrap doesn't have any handlers that help.
I've been able to get all the stylings to work (button type, icons, selected/deselected). But, I still can't get the click function to work:
<div class="btn-group">
<!-- Change button type based on status -->
<button type="button" class="btn btn-small dropdown-toggle" data-bind="css: {'btn-default' : status().statusName=='Matched', 'btn-danger' : status().statusName=='None', 'btn-info' : status().statusName=='Set'}" data-toggle="dropdown">
<!-- Add Glyph based on status -->
<span class="glyphicon" data-bind="css: {'glyphicon-ok' : status().statusName=='Matched', 'glyphicon-remove' : status().statusName=='None', 'glyphicon-list' : status().statusName=='Set'}"></span> <span data-bind="text: status().statusName"> </span> <span class="caret"></span>
</button>
<!-- Loop for status -->
<ul class="dropdown-menu" role="menu" data-bind="foreach: $root.availableStatus">
<!-- Disable item if selected -->
<li data-bind="css: {'disabled' : statusName==$parent.status().statusName}">
<!-- Not working -->
<a href="#" data-bind="click: $root.updateStatus"><span class="glyphicon" data-bind="css: {'glyphicon-ok' : statusName=='Matched', 'glyphicon-remove' : statusName=='None', 'glyphicon-list' : statusName=='Set'}"></span> <span data-bind="text: statusName"></span></a>]
I don't know if it can help you (i don't see refs in your fiddle) :
Modified JS Fiddle Example
Steps to reproduce
updateStatus
function on your$root
. You don't need it for this simple task.Bind the
click
event to$parent.status
.We want the
status
function (ako.observable
) on the currentArticle
to be called. At the point where the anchor is defined, the context parent is theArticle
, so you want to use$parent.status
. The current context, which is the element of$root.availableStatus
being clicked, is the argument that will be passed to thestatus
function.