Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery DatePicker and beforeShowDay

I am desperately trying to use this feature to enable only specific days in my datepicker, but the beforeShowDay function is never triggered :(

even this is not working:

$(document).ready(function(){
    /*initialisation des composants*/
    initComponent();
});


availableDates = new Array();

/* Fonction d'initialisation des composants */
function initComponent(){
    /* Date retrait */
    $("#dateRetrait").datepicker();

    $("#dateRetrait").datepicker({beforeShowDay: function(d) {
        console.log("bsd");
        alert("bsd");
    }});

    //$("#dateRetrait").datepicker({buttonImage: "../../../Images/boutons/btn_calendier.png"});
    //$("#dateRetrait").datepicker({showButtonPanel: true });
    //$("#dateRetrait").datepicker({beforeShow: function() {setTimeout(function() {$(".ui-datepicker").css("z-index", 9999999999);}, 10);}});

    $('#comboLieux').attr('disabled', 'disabled');
    $('#comboCreneau').attr('disabled', 'disabled');
    $('#dateRetrait').attr('disabled', 'disabled');
    $('#dateRetrait').datepicker('option', 'minDate', new Date());

    $("#dateRetrait").datepicker("option","dateFormat", 'dd-mm-yy');
}

If you have any ideas, I would greatly appreciate it!

in fact, even this is not working:

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>jQuery UI Datepicker - Restrict date range</title>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
    <script src="http://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css" />
    <script>
      $(function() {
        $( "#datepicker" ).datepicker({ minDate: -20, maxDate: "+1M +10D" });
        $( "#datepicker" ).datepicker({beforeShowDay: function(d) {
          console.log(d);
          alert(d);
        }});
      });
    </script>
  </head>
  <body>
    <p>Date: <input type="text" id="datepicker" /></p>
  </body>
</html>
like image 224
user1826124 Avatar asked Jan 23 '13 16:01

user1826124


1 Answers

According to jQueryUI's Datepicker API,

enter image description here

This explains why

$("#dateRetrait").datepicker({beforeShowDay: function(d) {
        console.log("bsd");
        alert("bsd");
    }});

does not work.

Also I noticed you are calling .datepicker() multiple times and each time you are giving it different parameters.

Instead of:

$("#dateRetrait").datepicker();

$("#dateRetrait").datepicker({beforeShowDay: function(d) {
        console.log("bsd");
        alert("bsd");
    }});

$('#dateRetrait').datepicker('option', 'minDate', new Date());

$("#dateRetrait").datepicker("option","dateFormat", 'dd-mm-yy');

Try doing this:

$("#dateRetrait").datepicker({
    dateFormat: 'dd-mm-yy',
    minDate: new Date(), 
    beforeShowDay: function(d) {
        var dmy = (d.getMonth()+1); 
        if(d.getMonth()<9) 
            dmy="0"+dmy; 
        dmy+= "-"; 

        if(d.getDate()<10) dmy+="0"; 
            dmy+=d.getDate() + "-" + d.getFullYear(); 

        console.log(dmy+' : '+($.inArray(dmy, availableDates)));

        if ($.inArray(dmy, availableDates) != -1) {
            return [true, "","Available"]; 
        } else{
             return [false,"","unAvailable"]; 
        }
    }
    });

I have also provided you with a demo: http://jsfiddle.net/yTMwu/18/ . Hope this helps!

like image 56
Dom Avatar answered Sep 21 '22 15:09

Dom