Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockoutjs (version 2.1.0): bind boolean value to select box

I want to bind boolean value to select element using KO v2.1.0, but obviously it doesn't work as expected.

HTML code:

<select data-bind="value: state">
    <option value="true">On</option>
    <option value="false">Off</option>
</select>

JavaScript code:

var model = {
    state: ko.observable(false)
};

ko.applyBindings(model)

So I expect the select box goes to "Off" position with the initial value false but it was at "On". If I put state: ko.observable("false") it will be correct but that's not I wanted. Anyone know how to bind the boolean value to select box with KO?

Jsfiddle: https://jsfiddle.net/greenlaw110/Ajm58/

like image 485
Gelin Luo Avatar asked May 09 '12 06:05

Gelin Luo


People also ask

What is KnockoutJS used for?

KnockoutJS is basically a library written in JavaScript, based on MVVM pattern that helps developers build rich and responsive websites.

How do you set a value in Ko observable?

To write a new value to the observable, call the observable and pass the new value as a parameter. For example, calling myViewModel. personName('Mary') will change the name value to 'Mary' . To write values to multiple observable properties on a model object, you can use chaining syntax.

What is data bind in KnockoutJS?

Knockout's declarative binding system provides a concise and powerful way to link data to the UI. It's generally easy and obvious to bind to simple data properties or to use a single binding.

What is two-way binding in KnockoutJS?

KO is able to create a two-way binding if you use value to link a form element to an Observable property, so that the changes between them are exchanged among them. If you refer a simple property on ViewModel, KO will set the form element's initial state to property value.


4 Answers

Here is an option that we explored for this one from this forum post:

ko.bindingHandlers.booleanValue = {
    init: function(element, valueAccessor, allBindingsAccessor) {
        var observable = valueAccessor(),
            interceptor = ko.computed({
                read: function() {
                    return observable().toString();
                },
                write: function(newValue) {
                    observable(newValue === "true");
                }                   
            });
        
        ko.applyBindingsToNode(element, { value: interceptor });
    }
};

So, we use a custom binding to inject a writeable computed observable between the UI and our view model. This is just an alternative to doing it directly in your view model.

Sample here: https://jsfiddle.net/rniemeyer/H4gpe/

like image 169
RP Niemeyer Avatar answered Oct 26 '22 01:10

RP Niemeyer


This happens because the select is working with strings, and not booleans at any stage. You should try a ko.computed( ... ) value instead.

Check here for details: https://jsfiddle.net/Ajm58/3/

<select id="testSelect" data-bind="value: stateString">
    <option value="true">true</option>
    <option value="false">false</option>
</select>
​

var model = {
    state: ko.observable(false)
};

model.stateString = ko.computed({
    read: function() { return (this.state() ? "true" : "false"); },
    write: function(value) { this.state(value == "true"); }
}, model);

ko.applyBindings(model);


setTimeout(function() {
    model.state(true);    
}, 1500);

setTimeout(function() {
    $("#testSelect").val("false");
}, 3000);
like image 38
Owen Berry Avatar answered Oct 26 '22 01:10

Owen Berry


Much easier than the other answers: use the options-Binding.

The answer is in the kind of lengthy expression below:

<select data-bind="options: [{text: 'On', value: true}, {text: 'Off', value: false}], value: lala1, optionsValue: 'value', optionsText: 'text'">

By using the options binding, you're able to assign virtually everything to the value of an option. It's rather uncommon, I guess, to bind an explicit value to the options binding, but in this 2-way case, it is reasonable.

like image 36
K. Rohde Avatar answered Oct 26 '22 01:10

K. Rohde


I think I got the answer, put the number "1" and "0" to the option value respectively:

<select data-bind="value: state">
    <option value="1">On</option>
    <option value="0">Off</option>
</select>

See https://jsfiddle.net/greenlaw110/Ajm58/2/

like image 28
Gelin Luo Avatar answered Oct 26 '22 00:10

Gelin Luo