I'm developing an application with angular 8 in which I have an input that contains only year number. I have used mat-datepicker and want only to choose year.
<mat-form-field class="input-control">
<input matInput placeholder="{{'enter'|translate}}.." [formControl]="form.controls.openedAt"
[matDatepicker]="date" readonly>
<mat-datepicker-toggle matSuffix [for]="date"></mat-datepicker-toggle>
<mat-datepicker #date startView="multi-year"></mat-datepicker>
</mat-form-field>
this code shows year view first, but when I choose the year, the month view will be opened, then days view.
So how can I select only year, or show only year view on mat-datepicker?
As you can see in the above code, we have provided ' format:"yyyy" ' (ex: 2020) and " viewMode:'years '", which will allow datepicker to show/select only year part and it will hide Months or date part. Note: From version 1.2 and above viewMode is changed to startView, for older versions use viewMode instead of startview in the above JS code.
Steps to format the dates in mat-datepicker input Create a custom date adapter (PickDateAdapter) by extending NativeDateAdapter. Import formatDate from @angular/common to change the datepicker input format. Add custom date picker formats (PICK_FORMATS).
Finally add date picker toggle button to display or hide calender popup by using mat-datepicker-toggle element. To change the icon of datepicker toggle button use mat-icon along with matDatepickerToggleIcon property inside mat-datepicker-toggle element. By default when we open calender pop up it will show the current month calender.
To change the icon of datepicker toggle button use mat-icon along with matDatepickerToggleIcon property inside mat-datepicker-toggle element. By default when we open calender pop up it will show the current month calender. Instead of that if we want to set the view to current year we can use startView property of mat-datepicker.
This feature is not fully implemented yet. You can see that here:
https://github.com/angular/components/issues/4853
For now you can try this:
https://stackblitz.com/edit/angular-mulitdate-picker-demo
I did like this:
html:
<mat-form-field
appearance="outline">
<mat-label>Ano</mat-label>
<input [(ngModel)]="selectYear" [formControl]="dateForm" matInput
[matDatepicker]="picker" />
<mat-datepicker-toggle matSuffix [for]="picker" ></mat-datepicker-
toggle>
<mat-datepicker
#picker
startView="multi-year"
(yearSelected)="chosenYearHandler($event, picker)"
></mat-datepicker>
</mat-form-field>
component:
export const MY_FORMATS = {
parse: {
dateInput: 'YYYY',
},
display: {
dateInput: 'YYYY',
monthYearLabel: 'YYYY',
monthYearA11yLabel: 'YYYY',
},
};
, providers: [ { provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS] },
{provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
]
chosenYearHandler(ev, input){
let { _d } = ev;
this.selectYear = _d;
input._destroyPopup()
}
Its works well and very simple!!!
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