简体   繁体   English

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

[英]How to show tooltip on disabled dates of jquery datepicker

I am trying to add tooltip on some disabled dates as holiday reason but unable get them displayed when hovered.我正在尝试在某些禁用日期添加工具提示作为假期原因,但无法在悬停时显示它们。 i have even tried adding custom hover function but no success.我什至尝试添加自定义悬停功能,但没有成功。

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);
    }
});

the holidays_dates variable consists of disabled date and reason and is perfectly adding the reason to the title of the element Holiday_dates 变量由禁用日期和原因组成,并且完美地将原因添加到元素的标题中

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

as above holiday reason added in title attribute of tag but hovering on that date does not show tooltip如上所述,在标签的标题属性中添加了假期原因,但悬停在该日期不显示工具提示

To disable the date and show tooltip use option beforeShowDay.要禁用日期和显示工具提示,请使用 ShowDay 之前的选项。

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]];
    }
}

and customize the css of the disabled date并自定义禁用日期的css

function changeDisableDateColor(){
    var unselectableDate = $('.ui-datepicker-unselectable a');

    unselectableDate.css({
        background: 'red',
        cursor: 'context-menu',
        border: 0
    });
}

call the above function ie changeDisableDateColor when date field is clicked.单击日期字段时调用上述函数,即 changeDisableDateColor。 $('#datepicker').click(function() { changeDisableDateColor(); });

and also inside onChangeMonthYear (detects the change in month or year) option以及 onChangeMonthYear(检测月份或年份的变化)选项内

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

setTimeout is used because the jquery ui removes all the dates item and add again, so we wait until we have right date.使用 setTimeout 是因为 jquery ui 删除了所有日期项并再次添加,所以我们等待直到我们有正确的日期。 Without setTimeout var unselectableDate = $('.ui-datepicker-unselectable a');没有 setTimeout var unselectableDate = $('.ui-datepicker-unselectable a'); will give previous date.将给出以前的日期。

Here is the jsfiddle: http://jsfiddle.net/Xx4GS/1225/这是 jsfiddle: http : //jsfiddle.net/Xx4GS/1225/

Hope I answered the question.希望我回答了这个问题。

Edited:编辑:

There was bug, when day or month is of single digit ie 1-9 then the date does not get matched and will not be disabled.有错误,当日或月是个位数时,即 1-9,那么日期不会匹配并且不会被禁用。

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

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

Fixed the bug by adding 0 in front of day and month.通过在日和月前添加 0 来修复错误。

Here is the solved js fiddle: http://jsfiddle.net/Lf5p3hct/2/这是解决的js小提琴: http : //jsfiddle.net/Lf5p3hct/2/

I had the same issue with JQuery 3.3.1.我在 JQuery 3.3.1 中遇到了同样的问题。 I solved this by just adding CSS.我通过添加 CSS 解决了这个问题。
Try adding the following in your CSS file:尝试在您的 CSS 文件中添加以下内容:

.ui-datepicker-calendar > .ui-state-disabled.Highlighted {
    pointer-events: initial;
}

This will remove the attribute pointer-events: none;这将删除属性pointer-events: none; that is set by the CSS class ui-state-disabled .这是由 CSS 类ui-state-disabled

Hightlighted part in the CSS has to be changed to whatever CSS class that you add to the date field in beforeShowDay() . CSS 中Hightlighted部分必须更改为您在beforeShowDay()添加到日期字段的任何 CSS 类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM