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

被刻印的时光 ゝ 提交于 2020-01-02 07:45:23

问题


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.


回答1:


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/



来源:https://stackoverflow.com/questions/19570109/dynamically-enable-disable-kendo-datepicker-with-knockout-kendo-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!