From ce0ce0f229e7cbff3f9a8f3a52584497591b0c37 Mon Sep 17 00:00:00 2001
From: Paul Ruston <60648319+paulo-rico@users.noreply.github.com>
Date: Tue, 27 May 2025 08:24:37 +0100
Subject: [PATCH] Supply Appointment data to SchedulerSlotRenderEventArgs -
#2050 (#2154)
* Supply Appointment data to SchedulerSlotRenderEventArgs
* Get appointments on demand
* Revert YearView SlotRender code to remove breaking change. Update demo
---
Radzen.Blazor/IScheduler.cs | 3 +-
Radzen.Blazor/RadzenScheduler.razor.cs | 4 +-
Radzen.Blazor/Rendering/DayView.razor | 2 +-
Radzen.Blazor/Rendering/MonthView.razor | 2 +-
Radzen.Blazor/Rendering/WeekView.razor | 2 +-
Radzen.Blazor/Rendering/YearPlannerView.razor | 2 +-
.../Rendering/YearTimelineView.razor | 2 +-
Radzen.Blazor/Rendering/YearView.razor | 2 +-
Radzen.Blazor/SchedulerSlotRenderEventArgs.cs | 8 ++++
.../Pages/SchedulerPlannerTimeline.razor | 46 +++++++++++++++++--
10 files changed, 61 insertions(+), 12 deletions(-)
diff --git a/Radzen.Blazor/IScheduler.cs b/Radzen.Blazor/IScheduler.cs
index 66294bf8..848a9dfc 100644
--- a/Radzen.Blazor/IScheduler.cs
+++ b/Radzen.Blazor/IScheduler.cs
@@ -100,8 +100,9 @@ namespace Radzen.Blazor
///
/// The start of the slot.
/// The end of the slot.
+ /// Function to return appointments for this range.
/// A dictionary containing the HTML attributes for the specified slot.
- IDictionary GetSlotAttributes(DateTime start, DateTime end);
+ IDictionary GetSlotAttributes(DateTime start, DateTime end, Func> getAppointments);
///
/// Renders the appointment.
///
diff --git a/Radzen.Blazor/RadzenScheduler.razor.cs b/Radzen.Blazor/RadzenScheduler.razor.cs
index 14669312..acbb2918 100644
--- a/Radzen.Blazor/RadzenScheduler.razor.cs
+++ b/Radzen.Blazor/RadzenScheduler.razor.cs
@@ -370,9 +370,9 @@ namespace Radzen.Blazor
}
///
- public IDictionary GetSlotAttributes(DateTime start, DateTime end)
+ public IDictionary GetSlotAttributes(DateTime start, DateTime end, Func> getAppointments)
{
- var args = new SchedulerSlotRenderEventArgs { Start = start, End = end, View = SelectedView };
+ var args = new SchedulerSlotRenderEventArgs { Start = start, End = end, getAppointments = getAppointments, View = SelectedView };
SlotRender?.Invoke(args);
diff --git a/Radzen.Blazor/Rendering/DayView.razor b/Radzen.Blazor/Rendering/DayView.razor
index f33f11e2..ad06ebd6 100644
--- a/Radzen.Blazor/Rendering/DayView.razor
+++ b/Radzen.Blazor/Rendering/DayView.razor
@@ -81,7 +81,7 @@
IDictionary Attributes(DateTime date)
{
- var attributes = Scheduler.GetSlotAttributes(date, date.AddMinutes(MinutesPerSlot));
+ var attributes = Scheduler.GetSlotAttributes(date, date.AddMinutes(MinutesPerSlot), () => AppointmentsInSlot(date, date.AddMinutes(MinutesPerSlot)));
attributes["class"] = ClassList.Create("rz-slot").Add("rz-slot-minor", (date.Minute / MinutesPerSlot) % 2 == 1).Add(attributes).ToString();
attributes["dropzone"] = "move";
return attributes;
diff --git a/Radzen.Blazor/Rendering/MonthView.razor b/Radzen.Blazor/Rendering/MonthView.razor
index 131df498..6aa78912 100644
--- a/Radzen.Blazor/Rendering/MonthView.razor
+++ b/Radzen.Blazor/Rendering/MonthView.razor
@@ -207,7 +207,7 @@
IDictionary Attributes(DateTime start)
{
- var attributes = Scheduler.GetSlotAttributes(start, start.AddDays(1));
+ var attributes = Scheduler.GetSlotAttributes(start, start.AddDays(1), () => AppointmentsInSlot(start, start.AddDays(1)));
attributes["class"] = ClassList.Create("rz-slot").Add(attributes).ToString();
attributes["dropzone"] = "move";
return attributes;
diff --git a/Radzen.Blazor/Rendering/WeekView.razor b/Radzen.Blazor/Rendering/WeekView.razor
index 02cfe99f..d5e1c287 100644
--- a/Radzen.Blazor/Rendering/WeekView.razor
+++ b/Radzen.Blazor/Rendering/WeekView.razor
@@ -96,7 +96,7 @@
IDictionary Attributes(DateTime date)
{
- var attributes = Scheduler.GetSlotAttributes(date, date.AddMinutes(MinutesPerSlot));
+ var attributes = Scheduler.GetSlotAttributes(date, date.AddMinutes(MinutesPerSlot), () => AppointmentsInSlot(date, date.AddMinutes(MinutesPerSlot)));
attributes["class"] = ClassList.Create("rz-slot").Add("rz-slot-minor", (date.Minute / MinutesPerSlot) % 2 == 1).Add(attributes).ToString();
attributes["dropzone"] = "move";
return attributes;
diff --git a/Radzen.Blazor/Rendering/YearPlannerView.razor b/Radzen.Blazor/Rendering/YearPlannerView.razor
index 0a4e7bb0..1664aa96 100644
--- a/Radzen.Blazor/Rendering/YearPlannerView.razor
+++ b/Radzen.Blazor/Rendering/YearPlannerView.razor
@@ -168,7 +168,7 @@
IDictionary Attributes(DateTime start, string className, bool slotInMonth)
{
- var attributes = Scheduler.GetSlotAttributes(start, start.AddDays(1));
+ var attributes = Scheduler.GetSlotAttributes(start, start.AddDays(1), () => AppointmentsInRange(start, start.AddDays(1)));
attributes["class"] = ClassList.Create(className).Add(attributes).ToString();
attributes["dropzone"] = "move";
if (!slotInMonth)
diff --git a/Radzen.Blazor/Rendering/YearTimelineView.razor b/Radzen.Blazor/Rendering/YearTimelineView.razor
index 1b5fae7a..90160788 100644
--- a/Radzen.Blazor/Rendering/YearTimelineView.razor
+++ b/Radzen.Blazor/Rendering/YearTimelineView.razor
@@ -158,7 +158,7 @@
IDictionary Attributes(DateTime start, string className, bool slotInMonth)
{
- var attributes = Scheduler.GetSlotAttributes(start, start.AddDays(1));
+ var attributes = Scheduler.GetSlotAttributes(start, start.AddDays(1), () => AppointmentsInRange(start, start.AddDays(1)));
attributes["class"] = ClassList.Create(className).Add(attributes).ToString();
attributes["dropzone"] = "move";
if (!slotInMonth)
diff --git a/Radzen.Blazor/Rendering/YearView.razor b/Radzen.Blazor/Rendering/YearView.razor
index 9ee76443..c76f8b15 100644
--- a/Radzen.Blazor/Rendering/YearView.razor
+++ b/Radzen.Blazor/Rendering/YearView.razor
@@ -101,7 +101,7 @@
IDictionary Attributes(DateTime start, string className)
{
- var attributes = Scheduler.GetSlotAttributes(start, start.AddDays(1));
+ var attributes = Scheduler.GetSlotAttributes(start, start.AddDays(1), () => AppointmentsInRange(start, start.AddDays(1)));
attributes["class"] = ClassList.Create(className).Add(attributes).ToString();
return attributes;
}
diff --git a/Radzen.Blazor/SchedulerSlotRenderEventArgs.cs b/Radzen.Blazor/SchedulerSlotRenderEventArgs.cs
index 534e9010..2efa8cad 100644
--- a/Radzen.Blazor/SchedulerSlotRenderEventArgs.cs
+++ b/Radzen.Blazor/SchedulerSlotRenderEventArgs.cs
@@ -17,6 +17,13 @@ namespace Radzen
/// The end of the slot.
///
public DateTime End { get; set; }
+ // used to pass a function to get appointments on demand.
+ internal Func> getAppointments;
+ private IEnumerable appointments;
+ ///
+ /// List of appointments.
+ ///
+ public IEnumerable Appointments => appointments ??= getAppointments();
///
/// HTML attributes to apply to the slot element.
///
@@ -25,5 +32,6 @@ namespace Radzen
/// The current view.
///
public ISchedulerView View { get; set;}
+
}
}
\ No newline at end of file
diff --git a/RadzenBlazorDemos/Pages/SchedulerPlannerTimeline.razor b/RadzenBlazorDemos/Pages/SchedulerPlannerTimeline.razor
index 80d7089d..613fc5fb 100644
--- a/RadzenBlazorDemos/Pages/SchedulerPlannerTimeline.razor
+++ b/RadzenBlazorDemos/Pages/SchedulerPlannerTimeline.razor
@@ -6,8 +6,8 @@
+TextProperty="Text" SelectedIndex="1"
+SlotSelect=@OnSlotSelect AppointmentSelect=@OnAppointmentSelect AppointmentRender=@OnAppointmentRender MonthSelect=@OnMonthSelect>
@@ -30,7 +30,33 @@
new Appointment { Start = DateTime.Today.AddHours(10), End = DateTime.Today.AddHours(12), Text = "Online meeting" },
new Appointment { Start = DateTime.Today.AddHours(10), End = DateTime.Today.AddHours(13), Text = "Skype call" },
new Appointment { Start = DateTime.Today.AddHours(14), End = DateTime.Today.AddHours(14).AddMinutes(30), Text = "Dentist appointment" },
- new Appointment { Start = DateTime.Today.AddDays(1), End = DateTime.Today.AddDays(12), Text = "Vacation" },
+ new Appointment { Start = DateTime.Today.AddDays(1), End = DateTime.Today.AddDays(7), Text = "Vacation" },
+ new Appointment { Start = DateTime.Today.AddDays(21).AddHours(10), End = DateTime.Today.AddDays(21).AddHours(11), Text = "Client #1" },
+ new Appointment { Start = DateTime.Today.AddDays(21).AddHours(11), End = DateTime.Today.AddDays(21).AddHours(12), Text = "Client #2" },
+ new Appointment { Start = DateTime.Today.AddDays(21).AddHours(12), End = DateTime.Today.AddDays(21).AddHours(13), Text = "Client #3" },
+ new Appointment { Start = DateTime.Today.AddDays(21).AddHours(13), End = DateTime.Today.AddDays(21).AddHours(14), Text = "Client #4" },
+ new Appointment { Start = DateTime.Today.AddDays(17).AddHours(10), End = DateTime.Today.AddDays(17).AddHours(11), Text = "Client #1" },
+ new Appointment { Start = DateTime.Today.AddDays(17).AddHours(11), End = DateTime.Today.AddDays(17).AddHours(12), Text = "Client #2" },
+ new Appointment { Start = DateTime.Today.AddDays(18).AddHours(10), End = DateTime.Today.AddDays(18).AddHours(11), Text = "Client #1" },
+ new Appointment { Start = DateTime.Today.AddDays(18).AddHours(11), End = DateTime.Today.AddDays(18).AddHours(12), Text = "Client #2" },
+ new Appointment { Start = DateTime.Today.AddDays(18).AddHours(12), End = DateTime.Today.AddDays(18).AddHours(13), Text = "Client #3" },
+ new Appointment { Start = DateTime.Today.AddDays(18).AddHours(13), End = DateTime.Today.AddDays(18).AddHours(14), Text = "Client #4" },
+ new Appointment { Start = DateTime.Today.AddDays(18).AddHours(14), End = DateTime.Today.AddDays(23).AddHours(15), Text = "Client #5" },
+ new Appointment { Start = DateTime.Today.AddDays(6).AddHours(10), End = DateTime.Today.AddDays(6).AddHours(11), Text = "Client #1" },
+ new Appointment { Start = DateTime.Today.AddDays(6).AddHours(11), End = DateTime.Today.AddDays(6).AddHours(12), Text = "Client #2" },
+ new Appointment { Start = DateTime.Today.AddDays(6).AddHours(12), End = DateTime.Today.AddDays(6).AddHours(13), Text = "Client #3" },
+ new Appointment { Start = DateTime.Today.AddDays(6).AddHours(13), End = DateTime.Today.AddDays(6).AddHours(14), Text = "Client #4" },
+ new Appointment { Start = DateTime.Today.AddDays(7).AddHours(10), End = DateTime.Today.AddDays(7).AddHours(11), Text = "Client #1" },
+ new Appointment { Start = DateTime.Today.AddDays(7).AddHours(11), End = DateTime.Today.AddDays(7).AddHours(12), Text = "Client #2" },
+ new Appointment { Start = DateTime.Today.AddDays(8).AddHours(10), End = DateTime.Today.AddDays(8).AddHours(11), Text = "Client #1" },
+ new Appointment { Start = DateTime.Today.AddDays(8).AddHours(11), End = DateTime.Today.AddDays(8).AddHours(12), Text = "Client #2" },
+ new Appointment { Start = DateTime.Today.AddDays(9).AddHours(10), End = DateTime.Today.AddDays(9).AddHours(11), Text = "Client #1" },
+ new Appointment { Start = DateTime.Today.AddDays(9).AddHours(11), End = DateTime.Today.AddDays(9).AddHours(12), Text = "Client #2" },
+ new Appointment { Start = DateTime.Today.AddDays(13).AddHours(10), End = DateTime.Today.AddDays(16).AddHours(11), Text = "Client #1" },
+ new Appointment { Start = DateTime.Today.AddDays(13).AddHours(11), End = DateTime.Today.AddDays(16).AddHours(12), Text = "Client #2" },
+ new Appointment { Start = DateTime.Today.AddDays(13).AddHours(12), End = DateTime.Today.AddDays(13).AddHours(13), Text = "Client #3" },
+ new Appointment { Start = DateTime.Today.AddDays(13).AddHours(13), End = DateTime.Today.AddDays(16).AddHours(14), Text = "Client #4" },
+ new Appointment { Start = DateTime.Today.AddDays(13).AddHours(14), End = DateTime.Today.AddDays(16).AddHours(15), Text = "Client #5" },
};
void OnSlotRender(SchedulerSlotRenderEventArgs args)
@@ -46,6 +72,20 @@
{
args.Attributes["style"] = "border-bottom: thick double var(--rz-base-600);";
}
+
+ // Color the slot depending on number of appointments for that day
+ if (args.Appointments.Count() > 4)
+ {
+ args.Attributes["style"] = "background: #ffabab;";
+ }
+ else if (args.Appointments.Count() > 2)
+ {
+ args.Attributes["style"] = "background: #ffc988;";
+ }
+ else if (args.Appointments.Count() > 0)
+ {
+ args.Attributes["style"] = "background: #c3ffc3;";
+ }
}
async Task OnSlotSelect(SchedulerSlotSelectEventArgs args)