Xsi
Xsi

Reputation: 97

Setting dynamic min and max date witt jquery datetimepicker

Based on earlier posts, I have setup the following custom binding for datetimepicker

ko.bindingHandlers.datetimepicker = {
    init: function (element, valueAccessor, allBindingsAccessor) {
        //initialize datepicker with some optional options
        var options = allBindingsAccessor().datetimepickerOptions || {};
        $(element).datetimepicker(options);

        //handle the field changing
        ko.utils.registerEventHandler(element, "change", function () {
            var observable = valueAccessor();
            try {
                observable($(element).datetimepicker("getDate"));//****
            }
            catch(ex) {}
        });

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

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

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

And dynamic min and max date custom binding handlers

ko.bindingHandlers.minDate = {
    update: function(element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor()),
        current = $(element).datetimepicker("option", "minDate", value);
    }
};


ko.bindingHandlers.maxDate = {
    update: function(element, valueAccessor) {
    var value = ko.utils.unwrapObservable(valueAccessor()),
        current = $(element).datetimepicker("option", "maxDate", value);
    }
};

I have two datetimepickers which limits each other in terms of min and max dates. The problem is that both datetimepicker closes immediately after an action (selecting a date or manipulating the slider). Removing the min-max handlers from the markup also removes the problem. Any suggestions on how to fix this? Thanks.

Upvotes: 2

Views: 898

Answers (1)

George Mavritsakis
George Mavritsakis

Reputation: 7083

I am not sure that you can alter min-max value of a datepicker while it is opened!

Changing the values of one probably fires observable update which forces an update on mix-max value of datepickers. I would check in your bindings the new value of min or max and if that has not changed i wouldn't alter the value of the datepicker.

This is probably the problem...

Upvotes: 1

Related Questions