简体   繁体   English

Knockout.js自定义引导程序datepicker绑定加值绑定

[英]Knockout.js custom bootstrap datepicker binding plus value binding

I need to attach a bootstrap datepicker to two input fields that also need a value binding since I need to be able to dynamically set the value of the input according to changes in my observable. 我需要将一个bootstrap datepicker附加到两个输入字段,这两个输入字段也需要一个值绑定,因为我需要能够根据我的observable的变化动态设置输入的值。

So far, the binding works only one way: When I pick a date in the datepicker, the observable is correctly updated. 到目前为止,绑定只有一种方式:当我在datepicker中选择一个日期时,observable正确更新。 But when I change the value of the attached observable in my viewmodel, the input does not reflect the change. 但是当我在viewmodel中更改附加的observable的值时,输入不会反映更改。

This is my binding handler: 这是我的绑定处理程序:

ko.bindingHandlers.bootstrapDP = {
    init: function (element, valueAccessor, allBindingsAccessor) {
        var options = allBindingsAccessor().datepickerOptions || {};
        $(element).bootstrapDP(options).on("changeDate", function (ev) {
            var observable = valueAccessor();
            observable($(element).val());
        });
    },
    update: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        $(element).bootstrapDP("setValue", value);
    }
};

In my viewmodel I have an object encapsulating startDate and endDate: 在我的viewmodel中,我有一个封装startDate和endDate的对象:

self.dateFilter = {
    startDate: ko.observable(),
    endDate: ko.observable()
};

This is my HTML: 这是我的HTML:

<input type="text" data-bind="bootstrapDP: dateFilter.startDate, value: dateFilter.startDate" />
<input type="text" data-bind="bootstrapDP: dateFilter.endDate, value: dateFilter.endDate" />

I'm using this datepicker library (in noConflict() -mode): https://github.com/eternicode/bootstrap-datepicker 我正在使用这个noConflict()库(在noConflict() ): https//github.com/eternicode/bootstrap-datepicker

DOCS here: http://bootstrap-datepicker.readthedocs.org/en/stable/ DOCS: http//bootstrap-datepicker.readthedocs.org/en/stable/

What will I need to add/change/do differently to get the desired result? 我需要添加/更改/做什么以获得所需的结果?

I believe you want to change this line: 我相信你想改变这一行:

$(element).bootstrapDP("setValue", value);

To: 至:

$(element).bootstrapDP("update", value);

This was taken from the bootstrap datepicker documentation . 这是从bootstrap datepicker 文档中获取的

The snippet below demonstrates this working. 下面的代码段演示了这一点。

 $.fn.bootstrapDP = $.fn.datepicker; ko.bindingHandlers.bootstrapDP = { init: function(element, valueAccessor, allBindingsAccessor) { var options = allBindingsAccessor().datepickerOptions || {}; $(element).bootstrapDP(options).on("changeDate", function(ev) { var observable = valueAccessor(); observable($(element).val()); }); }, update: function(element, valueAccessor) { var value = ko.utils.unwrapObservable(valueAccessor()); // use "update" instead of "setValue" $(element).bootstrapDP("update", value); } }; var vm = { startDate: ko.observable(), endDate: ko.observable() }; ko.applyBindings(vm); setTimeout(function(){ vm.startDate(new Date()) }, 1000); 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/css/bootstrap-datepicker3.standalone.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.4.0/js/bootstrap-datepicker.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <input type="text" data-bind="bootstrapDP: startDate, value: startDate" /> <input type="text" data-bind="bootstrapDP: endDate, value: endDate" /> 

So this is the working code I ended up with, just for those who need a quick working solution and don't want to work through the comments: 所以这是我最终得到的工作代码,仅适用于那些需要快速解决方案并且不想完成评论的人:

Custom binding handler: 定制绑定处理程序

ko.bindingHandlers.bootstrapDP = {
    init: function (element, valueAccessor, allBindingsAccessor) {
        var options = allBindingsAccessor().datepickerOptions || {};
        $(element).bootstrapDP(options);
    },
    update: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        $(element).bootstrapDP("update", value);
    }
};

Observables for the dates in the viewmodel: viewmodel中日期的可观察量:

self.dateFilter = {
    startDate: ko.observable(),
    endDate: ko.observable()
};

The HTML with the bindings: 带绑定的HTML:

<input type="text" data-bind="bootstrapDP: dateFilter.startDate, value: dateFilter.startDate" />
<input type="text" data-bind="bootstrapDP: dateFilter.endDate, value: dateFilter.endDate" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM