Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically change the month in Material Calendar <mat-calendar>

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!

like image 633
Csaba Avatar asked Aug 20 '19 08:08

Csaba


People also ask

How do I customize the Windows Forms monthcalendar control?

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.

How to show inline datepicker Calender in mat-calendar?

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.

How to change date manually in datepicker using datechange event?

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.

Why does the calendar look different on each version of calendar?

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.


4 Answers

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

like image 195
Dino Avatar answered Oct 22 '22 23:10

Dino


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>
like image 21
Simon_Weaver Avatar answered Oct 22 '22 22:10

Simon_Weaver


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
like image 21
Liviu Alexandru Bobocu Avatar answered Oct 23 '22 00:10

Liviu Alexandru Bobocu


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

like image 32
Cristian Zumelzu Avatar answered Oct 23 '22 00:10

Cristian Zumelzu