Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use mat-datepicker directly without input

I want to put the datepicker in a permanent sidebar always visible and not dependent on an input, is this possible? I imagined that just putting the component and adding opened = true could leave the datepicker inside a box always visible.

like image 393
Luis Ruiz Figueroa Avatar asked Feb 02 '18 12:02

Luis Ruiz Figueroa


People also ask

How do you use a mat Datepicker?

mat-datepicker exampleAdd a template reference variable mat-datepicker element. Connect mat-datepicker to input element via [matDatepicker] property. Finally add date picker toggle button to display or hide calender popup by using mat-datepicker-toggle element.

How can I get mat Datepicker value?

To use Datepicker in our application, we need to import MatDatepickerModule in application module. On this page we will create Datepicker and fetch selected value using NgModel , FormControl and FormGroup .

What is Mat Datepicker toggle?

A datepicker is composed of a text input and a calendar pop-up, connected via the matDatepicker property on the text input. There is also an optional datepicker toggle button that gives the user an easy way to open the datepicker pop-up.

What is Mat_date_locale?

Set Locale in Application Module using MAT_DATE_LOCALEThe default locale of Datepicker is changed by overriding the value of MAT_DATE_LOCALE . By default MAT_DATE_LOCALE uses the value of LOCALE_ID from @angular/core . The LOCALE_ID contains the default locale code. We can override MAT_DATE_LOCALE value as following.


2 Answers

turns out this is pretty straightforward import the MatDatePickerModule as per usual and then simply use the mat-calendar selector

<mat-calendar></mat-calendar> 

In order to hook into the selection via typescript

  @ViewChild(MatCalendar) _datePicker: MatCalendar<Date>  ngOnInit() {     this._datePicker.selectedChange.subscribe(x => {       console.log(x);     });   } 
like image 110
Brett Smith Avatar answered Sep 21 '22 15:09

Brett Smith


You can also try to hide with css ex:

<mat-form-field style="width:1px;visibility:hidden;">   <input matInput [matDatepicker]="picker" >   <mat-datepicker #picker></mat-datepicker> </mat-form-field> <button mat-button (click)="picker.open()" ></button> 

The advantage of hiding with css is that the datepicker is positioned relative to the hidden form-field

like image 37
Stefan Avatar answered Sep 20 '22 15:09

Stefan