Set minDate and maxDate on datetimepicker from ajax dataset

Senario:

Date input field (startmonth/endmonth) using jquery datetimepicker, and require to restrict users to pick invalid date from the picker. The data is retrieved from api.

  • The valid date is based on the ‘loaded’ date returned.
  • Once the startmonth selected, the endmonth should not be earlier than startmonth.


var $hierarchycontrols = $("select[data-link-name='AutomationReportFilter']", that.mainContainer);

var ajaxParameters = {
    methodUrl: "Reporting/GetAutomationReportFilter",
    dataInput: that.userInput
};

that.AjaxCall(ajaxParameters, function () {
    $hierarchycontrols.prop("disabled", true);
}, function (data) {
    $.each(data, function (i1, v1) {
        var count = 0;
        var currentName = v1.ParameterName;
        var $currentControl = $('select[data-filter-name="' + currentName + '"]', that.mainContainer);

        if (that.modifiedhiecontrolname !== currentName || $('#ddl' + that.modifiedhiecontrolname).val() == '') {
            $currentControl.html("");

            if (!$("#txt" + currentName).is(":hidden")) {
                if (v1.ParameterName == 'ar_startmonth' || v1.ParameterName == 'ar_endmonth') {
                    var mindate = '9999-12-31', maxdate = '';
                    $.each(v1.ParameterValue, function (i, v) {
                        mindate = mindate > v.Value ? v.Value : mindate;
                        maxdate = maxdate < v.Value ? v.Value : maxdate;
                    });
                    $("#txtar_startmonth").data("DateTimePicker").minDate(mindate);
                    $("#txtar_startmonth").data("DateTimePicker").maxDate(maxdate);
                    if (mindate < $("#txtar_startmonth").val()) mindate = $("#txtar_startmonth").val();
                    $("#txtar_endmonth").data("DateTimePicker").minDate(mindate);
                    $("#txtar_endmonth").data("DateTimePicker").maxDate(maxdate);
                }
                else {
                    $.each(v1.ParameterValue, function (i2, v2) {
                        $currentControl.append("<option " + (v2.Selected == true ? "selected" : "") + ' value="' + v2.Value + '">' +v2.Text + "");
                        count++;
                    });
                }
            }

            if (count > 1 || (count == 1 && $currentControl[0]!= undefined && $currentControl[0].textContent != '')) {
                $currentControl.prop("disabled", false);
            }
            } else {
                $currentControl.prop("disabled", false);
            }

       });

Ref:
project - BusinessSuite
file - FilterBuilder.js

发表评论