Search code examples
knockout.jskendo-uiknockout-kendo

Dynamically enable/disable kendo datepicker with Knockout-Kendo.js


I'm trying to enable / disable a kendo datepicker based on the selected value of an select using Knockout-Kendo.js.

The HTML:

<select data-bind="value: test">
    <option value="1">1</option>
    <option value="2">2</option>
</select>
<input data-bind="kendoDatePicker: {value: date, enabled: test() == 2}" />

The JS:

ko.applyBindings({
    date: ko.observable(),
    test: ko.observable(), 
});

The fiddle: http://jsfiddle.net/xTjqH/2/

It does initially disable the datepicker, but it wont enable it once "2" is selected.


Solution

  • Based on the way that dependencies are tracked for the individual options in the kendo bindings, you would need to represent your enabled condition with a computed. Otherwise, the test() == 2 is evaluated immediately and never again.

    With your sample, you could bind against a computed like dateEnabled:

    var viewModel = {
        date: ko.observable(),
        test: ko.observable(), 
    };
    
    viewModel.dateEnabled = ko.computed(function() {
       return viewModel.test() === "2"; 
    });
    

    Sample: http://jsfiddle.net/rniemeyer/JaVKt/