If you have technical questions, please create a support ticket in the DevExpress Support Center.
IEnumerable<DevExtreme.MVC.Demos.Models.DisableDatesAppointment>
Html.DevExtreme().Scheduler() (
.ID("scheduler")
.DataSource(Model)
.TextExpr("Text")
.StartDateExpr("StartDate")
.EndDateExpr("EndDate")
.Views(new[] { SchedulerViewType.WorkWeek, SchedulerViewType.Month })
.CurrentView(SchedulerViewType.WorkWeek)
.CurrentDate(new DateTime(2021, 4, 27))
.FirstDayOfWeek(FirstDayOfWeek.Sunday)
.StartDayHour(9)
.EndDayHour(19)
.ShowAllDayPanel(false)
.Height(730)
.DataCellTemplate(new JS("dataCellTemplate"))
.DateCellTemplate(new JS("dateCellTemplate"))
.TimeCellTemplate(new JS("timeCellTemplate"))
.OnContentReady("onContentReady")
.OnAppointmentFormOpening("onAppointmentFormOpening")
.OnAppointmentAdding("onAppointmentAdding")
.OnAppointmentUpdating("onAppointmentUpdating")
)
<script>
var dinnerTime = { from: 12, to: 13 };
var holidays = [
new Date(2021, 3, 29),
new Date(2021, 5, 6)
];
var ariaDescription = () => {
const disabledDates = holidays
.filter((date) => !isWeekend(date))
.map((date) => new Date(date).toLocaleDateString('en-US', {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
})
);
if (disabledDates?.length === 1) {
return `${disabledDates} is a disabled date`;
}
if (disabledDates?.length > 1) {
return `${disabledDates.join(', ')} are disabled dates`;
}
};
function dataCellTemplate(itemData, itemIndex, itemElement) {
var date = itemData.startDate;
var isDisabled = isHoliday(date) || isWeekend(date);
var element = $('<div />');
if (isDisabled) {
element.addClass('disable-date');
} else if (isDinner(date)) {
element.addClass('dinner');
}
return itemElement.append(element);
}
function dateCellTemplate(itemData, itemIndex, itemElement) {
var element = $('<div>' + itemData.text + '</div>');
if (isWeekend(itemData.date)) {
element.addClass('disable-date');
}
return itemElement.append(element);
}
function timeCellTemplate(itemData, itemIndex, itemElement) {
var element = $('<div>' + itemData.text + '</div>');
var date = itemData.date;
if (isDinner(date)) {
element.addClass('dinner');
}
if (hasCoffeeCupIcon(date)) {
element.append('<div class="cafe" />');
}
return itemElement.append(element);
}
function onContentReady(e) {
setComponentAria(e.component?.$element());
}
function onAppointmentFormOpening(e) {
var startDate = new Date(e.appointmentData.StartDate);
if(!isValidAppointmentDate(startDate)) {
e.cancel = true;
notifyDisableDate();
}
applyDisableDatesToDateEditors(e.form);
}
function onAppointmentAdding(e) {
if(!isValidAppointment(e.component, e.appointmentData)) {
e.cancel = true;
notifyDisableDate();
}
}
function onAppointmentUpdating(e) {
if(!isValidAppointment(e.component, e.newData)) {
e.cancel = true;
notifyDisableDate();
}
}
function notifyDisableDate() {
DevExpress.ui.notify("This date is disabled", "warning", 500);
}
function isValidAppointment(component, appointmentData) {
var startDate = new Date(appointmentData.StartDate);
var endDate = new Date(appointmentData.EndDate);
var cellDuration = component.option('cellDuration');
return isValidAppointmentInterval(startDate, endDate, cellDuration);
}
function isValidAppointmentInterval(startDate, endDate, cellDuration) {
var edgeEndDate = new Date(endDate.getTime() - 1);
if (!isValidAppointmentDate(edgeEndDate)) {
return false;
}
var durationInMs = cellDuration * 60 * 1000;
var date = startDate;
while (date <= endDate) {
if (!isValidAppointmentDate(date)) {
return false;
}
var newDateTime = date.getTime() + durationInMs - 1;
date.setTime(newDateTime);
}
return true;
}
function isValidAppointmentDate(date) {
return !isHoliday(date) && !isDinner(date) && !isWeekend(date);
}
function isHoliday(date) {
var localeDate = date.toLocaleDateString();
return holidays.filter(function(holiday) {
return holiday.toLocaleDateString() === localeDate;
}).length > 0;
}
function isWeekend(date) {
var day = date.getDay();
return day === 0 || day === 6;
}
function isDinner(date) {
var hours = date.getHours();
return hours >= dinnerTime.from && hours < dinnerTime.to;
}
function hasCoffeeCupIcon(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
return hours === dinnerTime.from && minutes === 0;
}
function applyDisableDatesToDateEditors(form) {
var startDateEditor = form.getEditor('StartDate');
startDateEditor.option('disabledDates', holidays);
var endDateEditor = form.getEditor('EndDate');
endDateEditor.option('disabledDates', holidays);
}
function setComponentAria(element) {
const prevAria = element?.attr('aria-label') || '';
element?.attr('aria-label', `${prevAria} ${ariaDescription()}`);
}
</script>
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
To implement this functionality in your application, follow the steps below:
-
Implement custom date/time validation functions
These functions should check whether a date or time is available. This demo includes the date/time validation functions listed below. In your application, they can be different.isValidAppointment
Prepares data and begins validation.isValidAppointmentInterval
Validates a date/time interval.isValidAppointmentDate
Calls theisHoliday
,isWeekend
, andisDinner
functions.isHoliday
/isWeekend
/isDinner
Check whether a date/time interval is a holiday, a weekend, or dinner time.
-
Validate appointments before they are created or updated
Before an appointment is created or updated, the Scheduler executes the onAppointmentAdding and onAppointmentUpdating functions. Use them to start validation. If a date or time is invalid,cancel
the creation or update. Implement the same logic in the onAppointmentFormOpening function so that users cannot open the appointment details form for a disabled date or time. -
Customize the appointment details form
The appointment details form includes two calendars that allow users to select an appointment's start and end date/time. You should also disable dates in these calendars. Review theapplyDisableDatesToDateEditors
function to see how this is done. Call this function from onAppointmentFormOpening. -
Customize the timetable appearance
Use templates to customize the appearance of data cells (dataCellTemplate), time cells (timeCellTemplate), and date cells (dateCellTemplate). In this demo, date cell customization is visible only in the Month view. To switch to this view, use the view switcher in the Scheduler's upper right corner.