Customized datepicker
I already described how to remove [x] in my blog, and now I want to talk about the Calendar widget or what it is called in JUI - Datepicker. The idea of adapting it to the WP calendar is not new, but the bad luck on Datepicker is that you cannot mark dates. That is, it is generally impossible to allocate a day except for the current and weekends. This is a big minus, and now I will show you how to turn this minus into a plus.
All you need to do is snoop on two calendar methods - the constructor and the table generator.
- (function($) {
-
- /**
- * autor: CTAPbIu_MABP
- * email: [email protected]
- * site: mabp.kiev.ua/2009/08/11/customized-datepicker
- * license: MIT & GPL
- * last update: 11.08.2009
- * version: 1.0
- */
-
- // сохраняем старые функции
- var old_datepicker = $.fn.datepicker;
- var old_generateHTML = $.datepicker._generateHTML;
-
- // и делигируем их новым
- $.datepicker._generateHTML = function(inst) {
- // получаем календарь ввиде raw-html
- var _generateHTML = old_generateHTML.apply(this, arguments),
- // выгребаем даты для этого календаря
- dates = inst.settings.hightlight.values;
- titles = inst.settings.hightlight.titles;
-
- // и начинаем расскрашивать
- for (var i in dates){
- if (dates[i].getFullYear() == inst.drawYear && dates[i].getMonth() == inst.drawMonth){
- _generateHTML = _generateHTML.replace(
- // магия регулярок
- new RegExp('' + dates[i].getDate() + '','i'),
- function(link, classes){
- // еще больше магии
- return link.replace(classes, classes + ' ui-state-custom' +
- (titles[i] ? '" title="'+ titles[i] : ''));
- });
- }
- }
- return _generateHTML;
- };
-
- // делегируем конструктор
- $.fn.datepicker = function(options){
- // новые опции преобразовываем к объекту
- options.hightlight = $.extend(
- {format:$.datepicker._defaults.dateFormat, values:[], titles:[], settings:{}},
- options.hightlight
- );
-
- // сразу превращаем даты в объекты типа Date для того чтобы сохранить
- options.hightlight.values = $.map(options.hightlight.values, function(value){
- return $.datepicker.parseDate(options.hightlight.format, value, options.hightlight.settings);
- });
-
- return old_datepicker.apply(this, [options]);
- };
- })(jQuery);
* This source code was highlighted with Source Code Highlighter.We don’t bother with the code there, but consider an example of use.
Add a new class in CSS. And create a date picker with new parameters.
.ui-state-custom {
border: 1px solid #f0f !important;
}
- $("div#datepicker").datepicker({ // inline datepicker
- firstDay: 1 , // первый день понедельник
- hightlight : { // подсвечиваем
- format:"dd/mm/yy", // формат в котором указаны даты, понимает все форматы которые понимает datepicker, по умолчанию равен $.datepicker._defaults.dateFormat или mm/dd/yy
- values:["1/08/2009","5/08/2009","15/08/2009"], // список дат в том формате который мы тока что указали в прошлом параметре
- titles:["Рас","Два","Три"], // если хотите можно указать список всплывающих подсказок для дат
- settings:{} // дополнительные параметры для функции преобразования строк в дату можно посмотреть в комментариях к коду датапикера
- }
- });
* This source code was highlighted with Source Code Highlighter.In action, you can look at my blog