0

Thanks of this post I know how to create a datepicker.

Unfortunately I couldn't make the datepicker always visible, not only when somebody click on the field.

I make a fork of original Fiddle with small modification here. Int this copy I have two inputs for Dates. I have a problem to make the datepicker visible all the time. Any idea how to fix it?

HTML

<input data-bind="datepicker: startDate, datepickerOptions: { minDate: new Date() }" />
<input data-bind="datepicker: endDate, datepickerOptions: { minDate: new Date() }" />

<hr />

<button data-bind="click: setToCurrentDate">Set To Current Date</button>

<hr />

<div data-bind="text: myDate"></div>

JS

ko.bindingHandlers.datepicker = {
    init: function(element, valueAccessor, allBindingsAccessor) {
        var $el = $(element);

        //initialize datepicker with some optional options
        var options = allBindingsAccessor().datepickerOptions || {};
        $el.datepicker(options);

        //handle the field changing
        ko.utils.registerEventHandler(element, "change", function() {
            var observable = valueAccessor();
            observable($el.datepicker("getDate"));
        });

        //handle disposal (if KO removes by the template binding)
        ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
            $el.datepicker("destroy");
        });

    },
    update: function(element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor()),
            $el = $(element),
            current = $el.datepicker("getDate");

        if (value - current !== 0) {
            $el.datepicker("setDate", value);   
        }
    }
};

var viewModel = {
    startDate: ko.observable(new Date("12/01/2013")),
    endDate: ko.observable(new Date("12/01/2013")),
    setToCurrentDate: function() {
        this.startDate(new Date());
        this.endDate(new Date());
    }
};

ko.applyBindings(viewModel);
Community
  • 1
  • 1
Kosmonaft
  • 1,286
  • 2
  • 17
  • 30

1 Answers1

1

To make the datepicker visible all the time bind it to a <div> instead of <input />.

<div data-bind="datepicker: myDate, datepickerOptions: { minDate: new Date() }"></div>
DaveB
  • 9,470
  • 4
  • 39
  • 66