So I have the following demo, http://dev.driz.co.uk/week.html , which shows the jQuery UI datpicker, which has multiple instances for each month of the year.
I changed it so that the user selects whole weeks, and then the start and end dates for these weeks are stored in the right sidebar with the week number.
I want to disable the dates when the user selected them so that they can see on the calendars which dates were selected (and also do not allow them to add the same date range more than once).
However, I donβt know where to start ... I created some functions for enabling and disabling dates, but I do not know how to actually disable dates using the beforeShowDay method.
For example:
var array = ["2013-03-14","2013-03-15","2013-03-16"] $('.week-picker').datepicker({ beforeShowDay: function(date){ var string = jQuery.datepicker.formatDate('yy-mm-dd', date); return [ array.indexOf(string) == -1 ] } });
But how would I turn off the date range? Since I only have start and end dates. And can I call beforeShowDay AFTER the datepicker is on the page, as in my example? And how can I turn on dates again?
Here is the code:
$(function() { var startDate; var endDate; var selectCurrentWeek = function() { window.setTimeout(function () { $('.week-picker').find('.ui-datepicker-current-day a').addClass('ui-state-active'); }, 1); } $('.week-picker').datepicker( { defaultDate: '01/01/2014', minDate: '01/01/2013', maxDate: '01/01/2015', changeMonth: false, changeYear: true, showWeek: true, showOtherMonths: true, selectOtherMonths: true, numberOfMonths: 12, onSelect: function(dateText, inst) { var date = $(this).datepicker('getDate'); startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay()); endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 6); var dateFormat = inst.settings.dateFormat || $.datepicker._defaults.dateFormat; addWeek($.datepicker.iso8601Week(new Date(dateText)), $.datepicker.formatDate( dateFormat, startDate, inst.settings ), $.datepicker.formatDate( dateFormat, endDate, inst.settings )); disableDates( $.datepicker.formatDate( dateFormat, startDate, inst.settings ), $.datepicker.formatDate( dateFormat, endDate, inst.settings )); selectCurrentWeek(); }, beforeShowDay: function(date) { var cssClass = ''; if(date >= startDate && date <= endDate) cssClass = 'ui-datepicker-current-day'; return [true, cssClass]; }, onChangeMonthYear: function(year, month, inst) { selectCurrentWeek(); } }); $('.week-picker .ui-datepicker-calendar tr').live('mousemove', function() { $(this).find('td a').addClass('ui-state-hover'); }); $('.week-picker .ui-datepicker-calendar tr').live('mouseleave', function() { $(this).find('td a').removeClass('ui-state-hover'); }); $('.remove').live('click', function(e){ enableDates($(this).attr('data-startdate'), $(this).attr('data-enddate')); $(this).parent('div').remove(); }); });
In short, there are two questions here ... How to disable dates AFTER the datapicker is added to the page. And secondly, how to turn off the range between two dates, since it looks like the beforeShowDay method expects an array of dates, not a range.