8

I try to disable some dates after I have initialised the Date Picker.

I am initialising the picker like this:

$( document ).ready(function() {
    $('#inputDatetime').pickadate({
        format: 'dd. mmmm yyyy',
        formatSubmit: 'yyyy-mm-dd',
        min: dt,
        selectYears: 2,
        selectMonths: true
    });
});

The user now performs some action that triggers an onChange() event. The disableDates() function prepares some more dates to disable and sets it to the picker using the set method:

function disableDates() {

    var disabledDays = [];

    $.ajax({  
        url: 'partners/getInactiveDays',
        dataType: 'json',
        async: false,
        success: function(data) {

            $.each(data.days, function(i, d) {
                disabledDays.push(parseInt(d.Day.id));
            });
        }
    });

    var $input = $('#inputDatetime').pickadate();
    var picker = $input.pickadate('picker');

    picker.set({
        disable: disabledDays
    });
}

Please note that in my case disableDays contains integers (reffering to weekdays) and dates according to the documentation (http://amsul.ca/pickadate.js/date/#disable-dates).

When the disableDates() function is triggered for the first time, it correctly disables the dates that I retrieved with AJAX. As soon as it is triggered a second time, the picker does not seem to be updated anymore. I guess the picker has to be rendered again somehow to reflect the changes. I tried to .stop() and .start() and .render(), without any effect.

How can I disable dates and refresh the picker correctly?

nimrod
  • 5,595
  • 29
  • 85
  • 149

4 Answers4

7

I couldn't let this go, so I've tested it quite extensively. In the end of the day, it works like it should. There is no need to stop, start or render it. The issue must be with your json-data, or the data in general (dates, ints).

I created two examples in jsfiddle, demonstrating that it does indeed work with the type of data you seem to be expecting.

I would advise you to log data.days, d.Day.id and finally disabledDays to console to check what they actually contain. If nothing else I hope you can have my fiddle examples as reference.

A note about the code though. The first time you initialize the picker you can assign it to a variable:

var myvar = $('#inputDatetime').pickadate({
    format: 'dd. mmmm yyyy',
    formatSubmit: 'yyyy-mm-dd',
    min: dt,
    selectYears: 2,
    selectMonths: true
});

And then, when you need to get this instance, you just do:

var picker = myvar.pickadate('picker');
picker.set('disable', [1,7]); //disables all saturdays & sundays

No need to reinitialize it in other words.

Also, a last note. Setting disabled dates doesn't clear previously set dates. You just keep adding to the collection of disabled dates, and when you use days you only have 1-7 to work with before everything is disabled.

Mackan
  • 6,200
  • 2
  • 25
  • 45
  • Hi @Mackan, thanks for this good answer. I managed to debug my code down to `picker.set('disable', disabledDays);`. This is the part where it stops working. The first call of the disableDates function works and the second call stops at this line. I have no idea why. The disableDays var contains correct data. I will try to find out why.. – nimrod May 05 '15 at 14:12
  • 1
    @doonot Are you sure you're only sending ints 1 to 7? It has to be the `disabledDays` that's causing issues (unless some other code is interfering)! Could you just put a `console.log(disabledDays)` above the actual call to `picker.set()`, or maybe you already have that.. – Mackan May 05 '15 at 14:40
  • No I am not only sending ints, the plugin can also handle dates: `dates to disable: 1,5,6,7,Mon May 11 2015 19:17:00 GMT+0200 (CEST),Tue May 12 2015 19:17:00 GMT+0200 (CEST),Wed May 13 2015 19:17:00 GMT+0200 (CEST),Thu May 14 2015 19:17:00 GMT+0200 (CEST),Fri May 15 2015 19:17:00 GMT+0200 (CEST),Sat May 16 2015 19:17:00 GMT+0200 (CEST),Sun May 17 2015 19:17:00 GMT+0200 (CEST),Sun Apr 26 2015 19:14:00 GMT+0200 (CEST),Mon Apr 27 2015 19:14:00 GMT+0200 (CEST),Tue Apr 28 2015 19:14:00 GMT+0200 (CEST),Wed Apr 29 2015 19:14:00 GMT+0200 (CEST),Thu Apr 30 2015 19:14:00 GMT+0200 (CEST),3` – nimrod May 05 '15 at 17:06
  • But I know that this works. If I run it the first time it gets disabled without any problems. The issues happen when I run it for the second time (I select another user and it calculates its absences). – nimrod May 05 '15 at 17:08
  • @doonot I don't get how you can use that code with date strings, since you use `parseInt()`. Also, is there any other code that gets run, that can potentially throw errors that halt the code. You need to share more if you want more assistance :) I can't see why it's failing. – Mackan May 05 '15 at 19:58
  • @doonot I've updated my fiddle example with date parsing. I hope you're doing something similar: http://jsfiddle.net/ctx8ec9L/3/ – Mackan May 05 '15 at 20:28
  • Hey. Thanks for the update, I am doing more or less the same and as I mentioned this is not the problem. I cannot reproduce it in JSFiddle, so I am going to accept your answer even though my problem is not solved yet :( thanks for your efforts. – nimrod May 06 '15 at 11:20
  • @doonot Thanks! I would love to help you solve it all the way, but the code-snippet you've provided works. So for me, or anyone else, to help you further requires that you share more code/details. Feel free to update you original question with more code, and I will at least try my best. – Mackan May 06 '15 at 13:15
  • How would you be able to clear previously set dates? – Panzercrisis Mar 23 '16 at 19:14
4

Make your changes from within the success/error handlers and try to enable all the dates before disabling:

function disableDates() {

    var disabledDays = [];
    var $input = $('#inputDatetime').pickadate();
    var picker = $input.pickadate('picker');

    $.ajax({  
        url: 'partners/getInactiveDays',
        dataType: 'json',
        async: false,
        success: function(data) {
            $.each(data.days, function(i, d) {
                disabledDays.push(parseInt(d.Day.id));
            });

            picker.set('enable', true);
            picker.set('disable', disabledDays);
        },
        error: function() {
            picker.set('disable', true);
        }
    });
}
mhu
  • 17,720
  • 10
  • 62
  • 93
0

¿Are you sure your ajax request is being called?

Some old, annoying, deprecated, not fully-functional browsers (yes, IE, I'm talking about you) would store ajax request in cache and return the first result in every subsequent request. As you have not marked your request as POST, it's possible partners/getInactiveDays is really only being called once.

Add method: 'POST' to your request, or append a random string (even POST request were sometimes stored in IE8) to your url as an unusued parameter (e.g. url:'partnets/getInactiveDays?randomizer='+new Date().getTime()) to prevent cache storing if they are not being excecuted.

Daniel
  • 209
  • 1
  • 3
  • 12
0

I know this is too late to answer, but just to share how I solved it. I just set the enable to true before the disable. Like so:

timePicker.set('enable', true)
timePicker.set('disable', disabledDays)
Blues Clues
  • 1,694
  • 3
  • 31
  • 72