Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select only year on mat-datepicker

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?

like image 407
walidtlili Avatar asked Feb 03 '20 10:02

walidtlili


People also ask

How to show/select only the year part in 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.

How to format the dates in mat-datepicker input?

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).

How to display or hide calender popup using mat datepicker?

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.

How to change the icon of mat-datepicker toggle button?

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.


2 Answers

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

like image 110
Tushar Avatar answered Nov 18 '22 13:11

Tushar


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!!!
like image 31
Welisson Caetano Avatar answered Nov 18 '22 14:11

Welisson Caetano