jQuery UI Datepicker与jQuery tipsy [英] jQuery UI Datepicker with jQuery tipsy

查看:103
本文介绍了jQuery UI Datepicker与jQuery tipsy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何想法如何在jQuery的UI Datepicker上实现 tipsy 工具提示?基本上,当用户在Datepicker中的特定日期移动时,我想获取一个工具提示。 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.

注意: / strong>
我的第一种方法是钩住 ui.datepicker onChangeMonthYear 事件,但是因为事件被触发,在更换日历中的日期之前, 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.

EASY HACK: strong>
将一个方法挂钩到您的日历的 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.

这篇关于jQuery UI Datepicker与jQuery tipsy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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