I need to show custom text on some dates at Jquery UI datepicker, to be more precise, to render a specific price for some dates. My idea was to use beforeShowDay to attach specific price in titles of such dates and then, in beforeShow to check out each td and to place title's text into the cell. Example:
var m, d, y, checkDate, specificPrice = '';
var specificPrices = {"2013-3-8":"300 EUR", "2013-2-26":"263 EUR"}
$('#my-datepicker').datepicker({
beforeShowDay: function checkAvailable(date) {
m = date.getMonth();
d = date.getDate();
y = date.getFullYear();
checkDate = y + '-' + (m+1) + '-' + d;
if(specificPrices[checkDate]){
specificPrice = specificPrices[checkDate];
}else{
specificPrice = '';
}
return [true, "", specificPrice];
},
beforeShow: function(elem, inst){
$('table.ui-datepicker-calendar tbody td', inst.dpDiv).each(function(){
var calendarPrice = $(this).attr('title');
if(calendarPrice != undefined){
$(this).find('a').append('<span class="calendar-price">' + calendarPrice + '<span>');
}
});
}
});
This will render prices on first calendar render (before month/year are changed) and for the inline calendar only.
I need prices to be shown on not-inline calendar and on any month/year change as well.
I tried some other variants, and tried to use onChangeMonthYear, but with no success so far.
Thank you for the attention, your ideas are welcome.
Based on @PrestonS's answer and this post, I have a nice simple solution that doesn't need to add
<style>
tags to header.My solution updates Preston's as such:
Modified
updateDatePickerCells
function:With this solution, you don't need the
addCSSRule
function at all.Modified css:
Initializing with the datepicker object
If you need the datepicker object in the
updateDatePickerCells
function, here is a way to get it at initialization time. (Based on this post)I came across this same scenario and thought I would post my solution for an inline datepicker but should work for a popup datepicker as well.
First of all, you cannot modify the contents of the table cells created by the datepicker plugin. Doing so will break its core functionality as it reads the cell content to generate the selected date string. So, the contents must be added using pure css.
Create your datepicker
Create the method to insert the cell content
Add a method to create new CSS rules on the fly, while keeping track of ones already created.
And finally, some default css for the new cell content
This works for basic content, but if you want to get fancy with something like '$20.95' (contains CSS special characters), you could use an md5 hash of the content to create the
className
variable.EDIT JSFiddle modified from @yuga's JSFiddle to include an MD5 hash of the unique class name for more complicated contents.