代码之家  ›  专栏  ›  技术社区  ›  Pavan Kumar T S

如何显示jquery datepicker禁用日期的工具提示

  •  2
  • Pavan Kumar T S  · 技术社区  · 6 年前

    我试图添加一些禁用日期的工具提示作为假日原因,但在悬停时无法显示这些日期。我甚至试过添加自定义悬停功能,但没有成功。

    availableDates = ["09/04/2018", "09/05/2018", "09/06/2018", "08/31/2018", "09/01/2018"];
    holidays_dates = { "09/02/2018": "sgsdf", "05/27/2018": "home sick", "08/20/2018": "fcg", "05/26/2018": "i dont know", "05/25/2018": "home sick" }
    
    function available(date) {
    
        var moth = String(date.getMonth() + 1);
        if (moth.length == 1) {
            moth = "0" + moth;
        }
    
        var dat = String(date.getDate());
        if (dat.length == 1) {
            dat = "0" + dat;
        }
        dmy = moth + "/" + dat + "/" + date.getFullYear();
    
        if ($.inArray(dmy, availableDates) != -1) {
            return [true, ""];
        } else if (dmy in holidays_dates) {
            console.log(dmy)
            return [false, "Highlighted", holidays_dates[dmy]];
        } else {
            return [false, ""];
        }
    }
    
    
    $("#datepicker").datepicker({
        beforeShowDay: function (dt) {
            return available(dt);
        },
        changeMonth: true,
        changeYear: true,
        onSelect: function (dateText) {
            getslots(dateText, selected_consultant);
        }
    });
    

    holidays_dates变量由禁用日期和原因组成,并将原因完美地添加到元素的标题中

    <td class=" ui-datepicker-unselectable ui-state-disabled Highlighted" title="home sick">
        <span class="ui-state-default">25</span>
    </td>
    

    如上所述,假日原因添加在标签的title属性中,但停留在该日期不显示工具提示

    1 回复  |  直到 6 年前
        1
  •  3
  •   Rojen    5 年前

    要禁用日期和显示工具提示,请在显示日期之前使用选项。

    beforeShowDay: function (date) {
        var formattedDate = $.datepicker.formatDate('mm/dd/yy', date),
            checkDisable = disabledDates.indexOf(formattedDate) == -1;
    
        if(checkDisable == true){
            return [checkDisable];
        }else{
        //add ui-datepicker-unselectable class to the disabled date.
          return [!checkDisable,'ui-datepicker-unselectable',holidays_dates[formattedDate]];
        }
    }
    

    自定义禁用日期的css

    function changeDisableDateColor(){
    var unselectableDate = $('.ui-datepicker-unselectable a');
    
    unselectableDate.css({
        background: 'red',
        cursor: 'context-menu',
        border: 0
    });
    

    }

    单击日期字段时调用上面的函数,即ChangeDisableDateColor。 $('#datepicker').click(function() { changeDisableDateColor(); });

    也在onchangemonthyear(检测月份或年份的变化)选项中

    onChangeMonthYear: function(){
      setTimeout(function(){
        changeDisableDateColor();
      });
    }
    

    使用setTimeout是因为jQueryUI删除了所有日期项并再次添加,所以我们要等到有了正确的日期。无设置超时 var unselectableDate = $('.ui-datepicker-unselectable a'); 将给出上一个日期。

    这是jsfiddle: http://jsfiddle.net/Xx4GS/1225/

    希望我能回答这个问题。

    编辑:

    有一个错误,当日或月是个位数,即1-9,那么日期不匹配,将不会被禁用。

    if(month.toString().length == 1){
        month = '0'+month.toString();
    }
    
    if(day.toString().length == 1){
        day = '0'+ day.toString();
    }
    

    通过在日前和月前添加0来修复错误。

    下面是解决的js小提琴: http://jsfiddle.net/Lf5p3hct/2/