I'm using the Material Calendar Component with custom navigation that I've built which shows the next 5 days after you select a date in the calendar.
When you select a date in the custom element it updates the Calendar with the newly selected date, but if the incoming date is in the next month, I would like the Calendar, to switch to that month automatically.
HTML:
<mat-calendar [selected]="selectedDate" (selectedChange)="onDateSelected($event)"
[minDate]="minDate" [dateClass]="dateClass"></mat-calendar>
TS:
@Input() set incomingDate(d: Date) {
this.selectedDate = d; // sets the incoming date on mat-calendar
this.dateClass(d); // highlights the next 5 days in the mat-calendar
this.calendar.updateTodaysDate(); // re-renders calendar to see changes
}
Is there any method that I can bind to the element to solve this problem? Thank you!
The Windows Forms MonthCalendar control allows you to customize the calendar's appearance in many ways. For example, you can set the color scheme and choose to display or hide week numbers and the current date. Set properties such as TitleBackColor, TitleForeColor and TrailingForeColor.
Now to show Inline Datepicker Calender we will use mat-calendar component with following option properties: selected: Set selected/ highlighted date in calendar datepicker. startAt: Date from which calender view will start. minDate: Set Minimum date in calendar datepicker. maxDate: Set Maximum date in calendar datepicker.
The (dateChange) is the change event to trigger when we change date manually in input box or select date from calendar. The dateChange is used in Datepicker input text as following. We have created handleDOBChange () method in our component. On date change, the handleDOBChange () will execute. Find its code.
This is because an updated version of the calendar is rendered with an appearance that is derived at run time from the current operating system theme. If you want to use these properties and enable the earlier version of the calendar, you can disable visual styles for your application.
You can use _goToDateInView
for that. It won't change the date object, it will just go to the month you pass as a parameter, therefore you can keep your date selection logic intact.
/** Handles year/month selection in the multi-year/year views. */
_goToDateInView(date: D, view: 'month' | 'year' | 'multi-year'): void;
And here is how you could use it:
Template
<mat-calendar #calendar></mat-calendar>
Component
@ViewChild('calendar', {static: false}) calendar: MatCalendar<Date>;
public goToDate() {
this.calendar._goToDateInView(someDateObjectInAnotherMonth, 'month')
}
Stackblitz
Use [startAt]
to jump automatically to the month of the initially set date.
<mat-calendar [minDate]="minDate"
[maxDate]="maxDate"
[startAt]="nextShipmentDt$ | async"
[selected]="nextShipmentDt$ | async"></mat-calendar>
you can try also the following. (worked for me after I checked the implementation -> MatCalendar).
this.calendar.activeDate = this.selectedDate; // set the active date to the selected date
this.calendar.updateTodaysDate(); // update calendar state
This workaround using Render (angular 10 is Render2) work for me
https://mariazacharia-k.medium.com/build-a-custom-calendar-with-angular-material-calendar-ebb6806308e5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With