I want to open calendar where user chooses date from date-picker not only when user clicks on calendar icon, but also when clicks on input field. Material DatePicker. So I create directive for this, attaching it to <md-datepicker>
and also watching for (click)
event on input:
Html:
<md-form-field class="field-half-width">
<input mdInput [mdDatepicker]="picker2" placeholder="Galioja iki"
formControlName="dateUntil" (click)="clickInp()">
<md-datepicker-toggle id="calendar" mdSuffix [for]="picker2" ></md-datepicker-toggle>
<md-datepicker #picker2 manualClickRenderer></md-datepicker>
</md-form-field>
form-component:
import {ManualClickRerender} from '../shared/directives/manual-click.directive';
@Component({
selector: 'form',
providers: [ManualClickRerender]
})
export class FormComponent implements OnInit, OnChanges {
...
clickInp() {
this.manualClickRerender.botClick();
}
Directive:
import { Directive, Renderer, ElementRef } from '@angular/core';
@Directive({
selector: '[manualClickRenderer]'
})
export class ManualClickRerender {
private nativeElement : Node;
constructor( private renderer : Renderer, private element : ElementRef ) {
this.nativeElement = element.nativeElement;
}
botClick() {
console.log(this.nativeElement); // logs the whole form, not the <md-datepicker>. I guess problem is here
this.renderer.invokeElementMethod(this.nativeElement, 'click', []);
}
}
This is my first custom directive ever, so not completaly sure how to import / provide it, etc. I imported it in main module and added to declarations, but after I imported it in my form component (I did that because need to pass click event to it) got error that there are no providers to ManualClickRerender. So added it as provider as well in form-component. So, yes, I imported it twice..
Also as you see, I use Rerender, not Rerender2, because it doesn't have invokeElementMethod
method... But I bet there is a workaround, just dont know it..
Thanks in advance for any info.
Connecting a date-picker to an inputThe date-picker is made up of text input and a calendar pop-up, which is linked via the mat-Date-picker property to the text input. It also has an optional date-picker toggle button that gives the user a simple method to open the date-picker pop-up window.
The problem is that the DOM template of the Component is not ready when you call the datepicker in the Component and so jquery can't bind the events to the DOM element. Try to use a timeout in your component. Note ` is missing before and after the template text.
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.
There is no need for providers
<md-input-container>
<input mdInput [mdDatepicker]="start" (click)="start.open()" [(ngModel)]="_start" [ngModelOptions]="{standalone: true}" placeholder="choisir date" (keydown)="false">
<button mdSuffix [mdDatepickerToggle]="start"></button>
</md-input-container>
<md-datepicker #start></md-datepicker>
You dont need a directive for that. Just add (click)="picker2.open()"
in the input field:
<md-form-field class="field-half-width">
<input mdInput [mdDatepicker]="picker2" placeholder="Galioja iki"
formControlName="dateUntil" (click)="picker2.open()">
<md-datepicker-toggle id="calendar" mdSuffix [for]="picker2" ></md-datepicker-toggle>
<md-datepicker #picker2></md-datepicker>
</md-form-field>
Here is a link to working demo.
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