JSFiddle - React, Tailwind, and code Playground
by rniemeyer
HTML
<!-- ko foreach: options -->
<input data-bind="checked: enabled" type="checkbox" />
<!-- /ko -->
<hr/>
<select data-bind="value: selected, options: options, optionsText: 'name', optionsValue: 'value', optionsBind: 'attr: { disabled: !enabled() }'">
</select>
<hr/>
<div data-bind="text: selected"></div>
JavaScript
ko.bindingHandlers.optionsBind = {
preprocess: function(value, key, addBinding) {
addBinding('optionsAfterRender', 'function(option, item) { ko.bindingHandlers.optionsBind.applyBindings(option, item, ' + value + ') }');
},
applyBindings: function(option, item, bindings) {
if (item !== undefined) {
option.setAttribute('data-bind', bindings);
ko.applyBindings(ko.contextFor(option).createChildContext(item), option);
}
}
};
ko.applyBindings({
selected: ko.observable(2),
options: [
{ value: 1, name: "one", enabled: ko.observable(true) },
{ value: 2, name: "two", enabled: ko.observable(true) },
{ value: 3, name: "three", enabled: ko.observable(false) }
]
});