@foreach (var nomeGiorno in _giorniSettimana) {
@nomeGiorno
} @for (var i = 0; i < StartDays; i++) {
} @for (var day = 1; day <= DaysInMonth; day++) { var currentDate = new DateTime(Date.Year, Date.Month, day); var events = GetEventsForDay(currentDate); var isToday = currentDate == DateTime.Today; var topRight = StartDays == 0 ? 7 : 7 - StartDays; var bottomLeft = DaysInMonth - (6 - EndDays);
@day @if (events.Any()) {
}
} @for (var i = 0; i < EndDays; i++) {
}
@code { [Parameter] public required DateTime Date { get; set; } [Parameter] public EventCallback DateChanged { get; set; } private List Events { get; set; } private int DaysInMonth { get; set; } private int StartDays { get; set; } private int EndDays { get; set; } readonly string[] _giorniSettimana = ["Lu", "Ma", "Me", "Gi", "Ve", "Sa", "Do"]; protected override void OnInitialized() { ChangeMonth(); } protected override void OnParametersSet() { ChangeMonth(); } private void ChangeMonth() { var firstDay = Date; DaysInMonth = DateTime.DaysInMonth(firstDay.Year, firstDay.Month); var dayOfWeek = (int)firstDay.DayOfWeek; StartDays = dayOfWeek == 0 ? 6 : dayOfWeek - 1; var tempTotalCell = (int)Math.Ceiling((double)(DaysInMonth + StartDays) / 7); var totalCell = tempTotalCell * 7; EndDays = totalCell - (DaysInMonth + StartDays); Events = [ new CalendarEvent { Date = DateTime.Today, Title = "Meeting", Time = "10:00" }, new CalendarEvent { Date = DateTime.Today.AddDays(2), Title = "Dentista", Time = "15:30" }, new CalendarEvent { Date = DateTime.Today.AddDays(5), Title = "Scadenza", Time = "Tutto il giorno" } ]; } private List GetEventsForDay(DateTime day) { return Events.Where(e => e.Date.Date == day.Date).ToList(); } public class CalendarEvent { public DateTime Date { get; set; } public string Title { get; set; } = ""; public string Time { get; set; } = ""; } }