带有 jQ​​uery 醉意的 j​​Query UI 日期选择器 [英] jQuery UI Datepicker with jQuery tipsy

查看:17
本文介绍了带有 jQ​​uery 醉意的 j​​Query UI 日期选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Any ideas how to implement tipsy tooltips over jQuery's UI Datepicker? Basically I want to get a tooltip when the user moves on a specific date in the Datepicker. The Datepicker will be displayed inline and always visible.

谢谢!

推荐答案

听起来很酷,

这是我的解决方案.阅读评论.

Here's my solution. Read the comments.

(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 方法也会在用户从可见月份中选择一天时调用,或者当您通过 datepicker('setDate', theDate) 设置日期时调用代码>,这可能有点低效.

  1. The _updateDatepicker method get's called also when the user selects a day form the visible month, or when you set a date via datepicker('setDate', theDate), which could be a little inefficient.

它依赖于 Datepicker 的一个私有函数,如果在未来的版本中他们决定改变它的功能,或者改变它的名字,这个代码就会中断.虽然由于函数的性质,我认为这不会很快发生.

It relies in a private function of the Datepicker, if in future versions they decide to change it's functionality, or change the name, this code will break. Although because of the nature of the function I don't see that happening soon.

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

NOTE: My first approach was to hook to the onChangeMonthYear event of the ui.datepicker, but because the event is triggered, before the replacing of the dates in the calendar, the addTipsys method, would add the tipsy's to the calendar dates that are about to get cleared. Therefore the need to call the addTipsys event AFTER the elements get refreshed.

轻松破解:将方法挂接到日历的 onChangeMonthYear 事件,并执行 setTimeout 来调用提示.需要进行一些验证.

EASY HACK: Hook a method to the onChangeMonthYear event of your calendar, and do a setTimeout to call the tipsy's. Some validation will need to be done.

这篇关于带有 jQ​​uery 醉意的 j​​Query UI 日期选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆