在jquery datepicker中禁用周末,假期

时间:2022-11-30 09:40:52

I'm using jquery ui date picker for selecting the start date and end date of a leave.when a user tries to select the leave commencing date i'm disabling holidays and the leave days which are already applied by the user.I'm getting those days from a database.so far it it is working fine.this is my code

我正在使用jquery ui日期选择器来选择请假的开始日期和结束日期。当用户尝试选择离开开始日期时,我将禁用假期和已经由用户应用的休假日。我是从数据库中获取那些日子。到目前为止它工作正常。这是我的代码

var unavailableDates=new Array(); //array of dates that should be disabled

//ajax function for getting already applied leave days and holidays

 function getUnavailable(){
 var rid=$('#rrid').val();

 var request=$.ajax({
    url: "../Controller/getUnavailableLeaveDays.php",
    type: "POST" ,
    data:{rid:rid},
    dataType: "json"

});
request.done(function(data){
    unavailableDates=data;  


});
request.fail(function(jgXHR,textStatus){
    alert ("Request failed:" + textStatus);
    return false;
});

}

function unavailable(date) {



 dmy = date.getFullYear()+  "-" + (date.getMonth()+1) + "-" +date.getDate() ;


 if ($.inArray(dmy, unavailableDates) < 0) {
    return [true,"","select"];

  } else {
    return [false,"","All ready Apllied for Leave or a holiday"];
  }
}

 $(function() {
 $( "#leave_commence" ).datepicker({
    defaultDate: "+1w",

    beforeShowDay: unavailable,
    dateFormat:"yy-mm-dd" ,
    numberOfMonths: 2,
    onClose: function( selectedDate ) {
        $( "#leave_ends" ).datepicker( "option", "minDate", selectedDate );
    }
 });
 $( "#leave_ends" ).datepicker({
    defaultDate: "+1w",

    beforeShowDay: unavailable,

    dateFormat:"yy-mm-dd" ,
    numberOfMonths: 2,
    onClose: function( selectedDate ) {
        $( "#leave_commence" ).datepicker( "option", "maxDate", selectedDate );
    }
   });
 });

Now i want to disable weekend from the date picker.I can use noWeekends() method in beforeShowDay.but i have already defined a function for beforeShowDay.So i tried to use the noWeekend() inside that function.but it is not working.What should i do now?please help me.

现在我想从日期选择器中禁用周末。我可以在beforeShowDay中使用noWeekends()方法。但我已经为beforeShowDay定义了一个函数。所以我试图在该函数中使用noWeekend()。但它无效。我现在该怎么办?请帮帮我。

1 个解决方案

#1


0  

Do not compare strings with dates, use valueOf() as explained here and see code below:

不要将字符串与日期进行比较,请使用此处说明的valueOf()并查看以下代码:

function unavailable(date) {
    dmy = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
    var found = false;
    for (var i in unavailableDates) {
        if (dmy.valueOf() == unavailableDates[i].valueOf()) {
            found = true;
        }
    }
    if (!found) {
        return [true, "", "select"];
    } else {
        return [false, "red2", "no"];
  }
 }

in CSS you can add:

在CSS中你可以添加:

.red2 {
    background-color:red
}

jsFiddle

#1


0  

Do not compare strings with dates, use valueOf() as explained here and see code below:

不要将字符串与日期进行比较,请使用此处说明的valueOf()并查看以下代码:

function unavailable(date) {
    dmy = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
    var found = false;
    for (var i in unavailableDates) {
        if (dmy.valueOf() == unavailableDates[i].valueOf()) {
            found = true;
        }
    }
    if (!found) {
        return [true, "", "select"];
    } else {
        return [false, "red2", "no"];
  }
 }

in CSS you can add:

在CSS中你可以添加:

.red2 {
    background-color:red
}

jsFiddle