Bootstrap datepicker with knockout.js databind

前端 未结 4 1784
耶瑟儿~
耶瑟儿~ 2020-12-04 22:25

This question is similar to knockoutjs databind with jquery-ui datepicker, but instead of the jQueryUI datepicker, I would like to use one of the Bootstrap datepickers.

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 22:58

    Here is a sample of how you could accomplish this with the datepicker that you are using:

    ko.bindingHandlers.datepicker = {
        init: function(element, valueAccessor, allBindingsAccessor) {
          //initialize datepicker with some optional options
          var options = allBindingsAccessor().datepickerOptions || {};
          $(element).datepicker(options);
    
          //when a user changes the date, update the view model
          ko.utils.registerEventHandler(element, "changeDate", function(event) {
                 var value = valueAccessor();
                 if (ko.isObservable(value)) {
                     value(event.date);
                 }                
          });
        },
        update: function(element, valueAccessor)   {
            var widget = $(element).data("datepicker");
             //when the view model is updated, update the widget
            if (widget) {
                widget.date = ko.utils.unwrapObservable(valueAccessor());
                if (widget.date) {
                    widget.setValue();            
                }
            }
        }
    };
    

    It did not look like there was any destroy functionality, so I removed that piece. This handles the widgets changeDate event to update the view model, when a user changes the date. The update function handles when the view model is changed to update the widget.

    If you want to bind the value to a non-observable, then it would take a little more code. Let me know if that is something that you need to support.

    http://jsfiddle.net/rniemeyer/KLpq7/

提交回复
热议问题