0

Thanks in advance for your help! I am trying to build a JQuery UI datepicker, where Days with events are disabled. Works great so far.

My problem is, that the processing of BeforeShowDay() can take rather long, so I'd like to disable user inputs during this time and display a Overlay. The overlay works fine, but when I click at the position of a button or date (while the overlay is displayed), the event will be fired right after the loading is complete and the overlay is being removed. I have been struggling to find out why, z-index is right and event.stopPropagation has brought no success. So I am reaching out for your help!

My Code:

    $("#datepickerloader").css("display", "block");  //<---- Here the overlay is added
    $.ajax({
        type: 'POST',
        url: '/pxxx/xxxxx',
        data: {"xxx":"XXXX"},
        success: function (re) {

            //processing of re removed

            if ($("#datepicker").hasClass("hasDatepicker")) {
                $("#datepicker").datepicker("refresh");
            } else {
                $("#datepicker").datepicker({
                    dateFormat: 'dd.mm.yy',
                    onSelect: function (date) {
                        displayTimes($(this).datepicker('getDate'));

                    },
                    beforeShowDay: function (date) {
                        if (typeof (allreturns[moment(date).format('YYYYMMDD')]) === "undefined" && typeof (monthlist[moment(date).format('YYYYMMDD')]) !== "undefined") {
                            var result = checkAvailability(date);
                            updateTimeSelectEvents();
                            return result;
                        } else if (typeof (allreturns[moment(date).format('YYYYMMDD')]) === "undefined" && monthlist === "empty") {
                            var result = checkAvailability(date);
                            updateTimeSelectEvents();
                            return result;
                        } else if (typeof (allreturns[moment(date).format('YYYYMMDD')]) !== "undefined") {
                            return allreturns[moment(date).format('YYYYMMDD')];
                        } else {
                            return [false, "", ""];
                        }
                    },
                    onChangeMonthYear: function (year, month) {
                        console.log("function called");
                        currentDisplayedMonth = month;
                        currentDisplayedYear = year;
                        //$("#possibleTimesContainer").html('');
                        if ($("#possibleTimesContainer").find(".open").length !== 0) {
                            $("#possibleTimesContainer").find(".open").removeClass("open");
                        }
                        var checkDate = (moment(new Date()).month(month - 1)).year(year).startOf('month');
                        if (typeof (allreturns[checkDate.format("YYYYMMDD")]) === "undefined") {
                            getAppointementsofMonth(month, year);
                        }


                    }
                });
            }
            console.log("removing overlay");
            $("#datepickerloader").css("display","none"); //<---- Here the overlay is removed
            updateTimeSelectEvents();
        }
    });

EDIT as Requested, here is the HTML & CSS

<div id="datepicker_container">
            <p><?php echo $this->translate("Datum") ?></p>
            <div id="datepickerloader"><i class="fa fa-spinner fa-pulse fa-2x"></i></div>
            <div id="datepicker"></div>

            <div id="possibleTimesContainer"></div>
        </div>

CSS:

#datepicker_container{
 margin: 0 auto;
 margin-bottom:50px;
 position: relative;
}

#datepickerloader{
 display: none;
 position: absolute;
 width: 100%;
 height: 100%;
 background-color: #d7d7d7;
 text-align: center;
 z-index: 100;
 opacity: 0.8;
 border-radius: 5px;
}
#datepickerloader i{
 position:absolute;
 top:50%;
}

Edit Snippet showing overlay over the datepicker. Issue is that any clicks while the overlay is in place and the picker is processing then run when the overlay is removed; this is not demonstrated in this snippet

$("#datepickerloader").css("display", "block"); //<---- Here the overlay is added

$("#datepicker").datepicker({
  dateFormat: 'dd.mm.yy',
});
#datepicker_container {
  margin: 0 auto;
  margin-bottom: 50px;
  position: relative;
}

#datepickerloader {
  display: none;
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: #d7d7d7;
  text-align: center;
  z-index: 100;
  opacity: 0.8;
  border-radius: 5px;
}

#datepickerloader i {
  position: absolute;
  top: 50%;
}
<link href="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.css" rel="stylesheet"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js"></script>

<div id="datepicker_container">
  <p>Date</p>
  <div id="datepickerloader"><i class="fa fa-spinner fa-pulse fa-2x"></i></div>
  <div id="datepicker"></div>
  <div id="possibleTimesContainer"></div>
</div>
freedomn-m
  • 21,261
  • 4
  • 28
  • 53
manuk
  • 1
  • 2
  • Can you include the css/html? Specifically for datepickerloader but ideally enough to reproduce the issue here. See [mcve]. – freedomn-m Sep 02 '20 at 15:03
  • Are you using `async:false` (doesn't look like it, and don't) - that might be the cause – freedomn-m Sep 02 '20 at 15:09
  • I have added the html and css, and I am not using async:false – manuk Sep 03 '20 at 08:50
  • Does this answer your question? [Jquery date picker z-index issue](https://stackoverflow.com/questions/7033420/jquery-date-picker-z-index-issue) – angel.bonev Sep 03 '20 at 08:54
  • 1
    I believe you have an **XY problem** - your *real* issue is that "*the processing of BeforeShowDay() can take rather long*" - there's no need for this. What does `updateTimeSelectEvents` do? Does this *have* to be run on every date displayed (unlikely as it doesn't have any parameters)? Also `moment(date).format('YYYYMMDD')` is run up to **5 times** (`moment` is meant for convenience not speed) - you should put this in a variable as the first line. – freedomn-m Sep 03 '20 at 09:01
  • What happens is your overlay is shown, your datepicker then starts processing and hogs the thread, any clicks have to wait for processing to finish, then overlay is hidden and process completes, releasing js for events (after overlay hidden). Try removing the overlay in setTimeout: `setTimeout(function() { $("#datepickerloader").css("display","none") } , 100);` – freedomn-m Sep 03 '20 at 09:05
  • 1
    Full ack on the previous comments, that “long running” part should be optimized to begin with. If that still doesn’t resolve the issue, and you still need your overlay - then perhaps try adding `pointer-events: none` for the `#datepicker` element while your overlay is shown. – CBroe Sep 03 '20 at 09:12

0 Answers0