components icon indicating copy to clipboard operation
components copied to clipboard

datepicker: (change) event does not work in mdInput

Open da45 opened this issue 9 years ago • 32 comments

Bug

change event does not work

<md-input-container>
    <input mdInput [mdDatepicker]="picker" (change)="doSomething($event)">
     <button mdSuffix [mdDatepickerToggle]="picker"></button>
</md-input-container>

<md-datepicker #picker></md-datepicker>    

What is the expected behavior?

change event must work

What is the current behavior?

when choose a date from datepicker no (change) event is triggered

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

Angular 4.1.2 & material 2.0.0-beta.5

da45 avatar May 17 '17 09:05 da45

what about this: <md-datepicker #picker (selectedChange)="updateDate()">

this is not working either. Here's the code from @angular/material

MdCalendar.propDecorators = { 'startAt': [{ type: Input },], 'startView': [{ type: Input },], 'selected': [{ type: Input },], 'minDate': [{ type: Input },], 'maxDate': [{ type: Input },], 'dateFilter': [{ type: Input },], 'selectedChange': [{ type: Output },], };

What event can I hook into when a date is selected?

mohi86 avatar May 18 '17 10:05 mohi86

@mohi86 mohi86 If your method is just to update the date, try the following binding: [(ngModel)]="myDate" , For my case, I really have to call a function to run some complex tasks,

da45 avatar May 18 '17 10:05 da45

@da45 Thanks, but I have the same issue, I need to run some calculations when the date is selected. I used angular2-material-datepicker but it's not working well with the angular material form elements.

mohi86 avatar May 18 '17 23:05 mohi86

the binding: [(ngModel)]="myDate" , works for me if I pick the date in the UI, but date modification made directly in the input field is not taken into account.

RobertBakic avatar May 19 '17 09:05 RobertBakic

After further test, the binding problem described in my previous post was related to the localization (French) of the date format (dd/mm/yyyy) instead of (mm/dd/yyyy) wich works fine.

RobertBakic avatar May 19 '17 10:05 RobertBakic

@da45 @mohi86 For now, you can split out the [(ngModel)] like this

<input [ngModel]="myDate" (ngModelChange)="updateMyDate($event)">
updateMyDate(newDate) {
  this.myDate = newDate;
  this.runOtherCalculations(newDate);
}

https://plnkr.co/edit/aZKLaA5qlZj71AqD5ZFz?p=preview

willshowell avatar May 23 '17 16:05 willshowell

while waiting for the final fix, idea of @willshowell is helpful,

da45 avatar May 24 '17 15:05 da45

Unfortunately, material 2.0.0-beta.6 was released but without fixing this issue :( ,

da45 avatar May 26 '17 14:05 da45

The following works for me. Note the spelling of the event!

<md-datepicker #picker (selectedChanged)="updateDate($event)">

jymdman avatar May 26 '17 21:05 jymdman

Hello guys,

@mohi86 mentioned that he used (selectedChange) event but it doesn't work, me too. In the latest material 2 release, you can use (selectedChanged) instead of (selectedChange).

<md-datepicker #picker (selectedChanged)="update($event)">

Close this issue, now it works.

gtzinos avatar May 29 '17 06:05 gtzinos

Hi,

@gtzinos @da45 @mmalerba , I can confirm this is working as of version 2.0.0-beta.6. Thanks

mohi86 avatar May 29 '17 08:05 mohi86

Hi @mohi86, i found a new issue, which inside the event (if you use ngModel in your datepicker input) your model will not be up to date. So one workaround is to get your data from event and to save them back to model or just to use the event data.

#4858

gtzinos avatar May 29 '17 09:05 gtzinos

I would say this issue is solved using the selectedChanged event. However, would be nice to (also?) have the change event. All other inputs use change, why suddenly the selectedChanged event?

pdeveltere avatar Jun 20 '17 13:06 pdeveltere

I am getting value of datepicker by $event mentioned as argument in selectedChanged event, it's fine. But in case of reactive forms, we get all values of inputs by the reactive form controls, but I am not able to get value of datepicker by reactive form control.

So, value of datepicker should also be get by reactive form control.

sahil-developer-88 avatar Jun 21 '17 10:06 sahil-developer-88

@sahil-developer-88 I'm not sure I follow.. I don't think getting the value with reactive forms is related, and seems to work just fine:

http://plnkr.co/edit/5Eqk4nDWrYUvnwYwzeK7?p=preview

willshowell avatar Jun 21 '17 13:06 willshowell

@willshowell, I didn't mean that you mentioned above in your code. I will share one example with you soon...Thanks

sahil-developer-88 avatar Jun 21 '17 17:06 sahil-developer-88

selectedChanged event is working fine for me when I select a date with a mouse.

However, if I manually change the date I need to be able to fire change or blur event which is not working currently.

https://plnkr.co/edit/rphB4cgzUmFG7Insfpqx?p=preview

Is there a workaround for this and will this be officially implemented?

EDIT:

<md-input-container>
  <input mdInput [mdDatepicker]="picker" placeholder="Choose a date" (change)="called(2)">
  <button mdSuffix [mdDatepickerToggle]="picker"></button>
</md-input-container>
<md-datepicker #picker (selectedChanged)="called(1)" ></md-datepicker>

Change/blur event works on the input tag.

ashaykolhe avatar Jun 26 '17 09:06 ashaykolhe

I am seeing the same behavior as @ashaykolhe . selectedChanged works only if date is selected using mouse.

playground0076 avatar Jun 27 '17 14:06 playground0076

@willshowell, the issue related to reactive form control is resolved after updating angular material, can you please tell me how can i update already selected datepicker value by clicking of another button click event?

sahil-developer-88 avatar Jun 28 '17 12:06 sahil-developer-88

+1 i encounter the same issue

wahello avatar Jun 29 '17 13:06 wahello