【问题标题】:jQuery UI Datepicker with jQuery tipsyjQuery UI Datepicker with jQuery Tipsy
【发布时间】:2010-02-19 22:38:07
【问题描述】:

任何想法如何在 jQuery 的 UI Datepicker 上实现 tipsy 工具提示?基本上,当用户在 Datepicker 中的特定日期移动时,我想获得一个工具提示。 Datepicker 将内联显示并始终可见。

谢谢!

【问题讨论】:

    标签: jquery user-interface datepicker tooltip


    【解决方案1】:

    听起来很酷,

    这是我的解决方案。阅读 cmets。

    (function($){
    
    
    /** 
     * Returns a dictionary, where the keys are the day of the month, 
     * and the value is the text.
     * @param year - The year of the events.
     * @param month - The month of the events.
     * @param calendarID - Events for a specific calendar.
     */
    function getMonthEvents(year, month, calendarId){
      return {11: "My birthday.", 23: "My anniversary" };
    }
    
    // Receives January->1
    function addTipsys(year, month, calendarId){
       var theEvents = getMonthEvents(year, month, calendarId);
       var theDateLinks = $('#' + calendarId + ' .ui-datepicker-calendar a');
       for(eventDay in theEvents){
          // Minus one, because the date in the tipies are regular dates (1-31)
          // and the links are 0-based.
          theDateLinks.eq(eventDay-1)  // select the right link
             .attr('original-title', theEvents[eventDay])  // set the text
             .tipsy();   // init the tipsy, set your properties.
       }
    }
    
    // Because the the event `onChangeMonthYear` get's called before updating 
    // the items, we'll add our code after the elements get rebuilt. We will hook 
    // to the `_updateDatepicker` method in the `Datepicker`.
    // Saves the original function.
    var _updateDatepicker_o = $.datepicker._updateDatepicker;
    // Replaces the function.
    $.datepicker._updateDatepicker = function(inst){ 
       // First we call the original function from the appropiate context.
       _updateDatepicker_o.apply(this, [inst]); 
       // No we can update the Tipsys.
       addTipsys(inst.drawYear, inst.drawMonth+1, inst.id);
    };
    
    // Finally the calendar initializer.
    $(function(){
       // Creates the date picker, with your options.
       $("#datepicker").datepicker();
       // Gets the date and initializes the first round of tipsies.
       var currentDate = $('#datepicker').datepicker('getDate');
       // month+1 because the event considers January->1
       // Last element is null, because, it doesn't actualy get used in the hanlder.
       addTipsys(currentDate.getYear(), currentDate.getMonth()+1, 'datepicker');
    });
    
    })(jQuery);
    

    不便之处:

    1. _updateDatepicker 方法 get 也会在用户从可见月份中选择一天,或者当您通过 datepicker('setDate', theDate) 设置日期时调用,这可能会有点低效。

    2. 它依赖于 Datepicker 的私有函数,如果在将来的版本中他们决定更改其功能或更改名称,则此代码将中断。尽管由于该功能的性质,我认为这种情况不会很快发生。

    注意: 我的第一种方法是挂钩ui.datepickeronChangeMonthYear 事件,但由于该事件被触发,在替换日历中的日期之前,addTipsys 方法会将醉酒添加到日历日期中即将被清除。因此需要在元素刷新后调用addTipsys 事件。

    简易破解: 将方法连接到日历的onChangeMonthYear 事件,并执行 setTimeout 来调用醉酒的人。需要进行一些验证。

    【讨论】:

    • 您好,谢谢!看来你在这里付出了一些努力,我很感激。我仍然有一些问题来实现这一点。我在addTipsys(year, month, inst.id); 上不断收到“年份”未定义错误,也许我做错了什么。你能把你的测试文件上传到某个地方让我看看吗?再次感谢!
    • 哦,是的,我的错……在$.datepicker._updateDatepicker 函数中将addTipsys(year, month, inst.id) 更改为addTipsys(inst.drawYear, inst.drawMonth+1, inst.id),抱歉我没有测试文件。我在 Firebug 控制台工作。
    • 嘿,是的,它确实有效!太感谢了!我现在需要提示提示以保持可见(即使用户将鼠标悬停在提示上),直到用户移动到另一个提示。我打算在工具提示中放置一些链接,希望我能用醉醺醺的方式做到这一点。否则我将使用另一个 jquery 工具提示插件。再次感谢您的帮助伙伴!
    • 太棒了!而且,您仍然可以将它与任何其他工具提示插件一起使用,只需在 addTipsys 事件中进行更改。很高兴我能帮上忙。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    相关资源
    最近更新 更多