Add Footer to Angular Material Datepicker

Viewed 2568

I am trying to implement a "Today" button on the Angular Material datepicker, but it must be positioned after the content.

enter image description here

After reading the API, it seems that, while it is straight-forward to customize the header of the datepicker by adding [calendarHeaderComponent]="myComponent", there are no options to add or customize a footer.

I am currently trying to make it work on top of the custom header example:

https://stackblitz.com/edit/angular-3xdhb1?file=src/app/datepicker-custom-header-example.ts

Also, it seems that in this issue, a similar feature is proposed.

5 Answers

I ended up adjusting the other answer. I am not too happy with the implementation, but it works without being too complex. A further feature would be to correctly select the datepicker to append the buttons to (since there may be multiple).

The end result would look something like:

https://stackblitz.com/edit/angular-c9xqse-pnrpgw

Above answers does not say that after adding extra elements to DatePicker or DateRangePicker using renderer2 and appendChild(), it won't allow for appropriate keyboard accessibility since extra added elements won't get focus as they should. The focus is trapped inside mat-calendar, which uses the cdkTrapFocus.

enter image description here

Since @angular-material v11.2.12 there is possibility to customize Confirmation Action Buttons. Due to this, it is no longer needed the usage of renderer2 and appendChild() in order to customize footer.

Together with Confirmation Action Buttons and Customizing the calendar header it is possible to add extra buttons, labels, texts etc. to header and the footer.

Here is the example, both with edited footer and header.

At my work we did something like that to add a timepicker to the datePicker. You can add the button by using .appendChild() or .append().

The idea is to append a component into the datepicker is I remember well. I'm not good enough in angular to reproduce it correctly but here is a basic repro of the concept on stackblitz

Also, i found enter link description here that seems accurate for your needs.

Hope it helps !

I solved this using mat-datepicker-actions

<input class="form-control " title="Data" matInput [matDatepicker]="picker" formControlName="date">
          <mat-datepicker-toggle class=" my-auto" matSuffix [for]="picker"></mat-datepicker-toggle>
          <mat-datepicker #picker>
            <mat-datepicker-actions class="">
              <button title="today" class="btn button" (click)="setTodayDate()" matDateRangePickerApply>
                [Hoje]
              </button>
            </mat-datepicker-actions>
          </mat-datepicker>

result image

Related