Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(select) event for NgbInputDatepicker #2181

Closed
floisloading opened this issue Feb 21, 2018 · 10 comments
Closed

(select) event for NgbInputDatepicker #2181

floisloading opened this issue Feb 21, 2018 · 10 comments

Comments

@floisloading
Copy link

Feature Request

(select)-Output on the -Element on which the NgbInputDatepicker is bound to.

Right now, there is no way to detect if a user has selected a date from the datepicker: https://stackoverflow.com/questions/48903442

Link to minimally-working plunker that reproduces the issue:

http://plnkr.co/edit/DdoP63tqccsItPNZOG4n?p=preview

Version of Angular, ng-bootstrap, and Bootstrap:

Angular: 5.2.0

ng-bootstrap: 1.0.0-beta.9

Bootstrap: 4.0.0

@maxokorokov
Copy link
Member

Also related to #1984

@maxokorokov
Copy link
Member

Might not be a good idea to use(select) maybe, as it already exists on the input.

I suggest we add the following:

@Output() dateSelect = new EventEmitter<NgbDateStruct>();
<input ngbDatepicker (dateSelect)="onDateSelect($event)" />

Also deprecate existing (select) event and rename it to (dateSelect) on the non-input datepicker for consistency

maxokorokov added a commit to maxokorokov/ng-bootstrap that referenced this issue Feb 26, 2018
@connormlewis
Copy link

Is there any workaround for this until the dateSelect output makes it into a release?

@floisloading
Copy link
Author

@connormlewis (ngModelChange) is triggered on dateSelect. However, it is also triggered on every keystroke when entering a date via keyboard: https://stackoverflow.com/questions/48903442

@connormlewis
Copy link

@frot-io I tried using that but didn't have success in my specific case. My ngb-datepicker is on an input in a reactive form, and when I triggered a function using (ngModelChange), the value of the reactive form had not yet been updated to the value selected in the date picker.

I eventually got around this by listening to the .valueChanged observable inside the form, which seems to give the correct value after a date is picked.

@floisloading
Copy link
Author

@connormlewis could you give a plunker example?

@connormlewis
Copy link

@frot-io Yep, here's a link http://plnkr.co/edit/1w2j3iIK1tmbPHxx7Yro?p=preview

@floisloading
Copy link
Author

@connormlewis I had almost the same issue, but with template-driven forms. I got it solved by putting (ngModelChange) before [(ngModel)] but that couldn't work here.

My guess is that you shouldn't use (ngModelChange) for reactive forms because of concurrency. Instead there is an Observable on which one can subscribe. I updated the plunker: http://plnkr.co/edit/wGqmUiTd8SQZi5yhcQx4?p=preview

@robeverett
Copy link

@frot-io I tried using that but didn't have success in my specific case. My ngb-datepicker is on an input in a reactive form, and when I triggered a function using (ngModelChange), the value of the reactive form had not yet been updated to the value selected in the date picker.

I eventually got around this by listening to the .valueChanged observable inside the form, which seems to give the correct value after a date is picked.

Hi
Could you show me how you listen to 'valueChanged' in Angular code.
I'm assuming this issue still exists as at Angular 7.0.3, but can never find any consistent info all in one place!

There is little info for example on using reactive forms with bootstrap directives - it's usually ([ngModel]).

I managed to find some interesting validation code elsewhere on here, though it is inadequate as it only tests for a string: it should really validate against the NgbDate object, I'd have thought:

this.reportForm.controls['dateFromPicker'].valueChanges.subscribe(data => {

  this.fromDate = data;
  this.showToDatePicker = true;     

  if (!data || (typeof data === 'string' && data.length == 0)) {
    this.reportForm.patchValue({
      dateFromPicker: null
    }, { emitEvent: false });

    this.invalidDates = true;
  }
});

@floisloading
Copy link
Author

I'm not working in this project anymore, so I cannot show you code examples... I think my code was similar to yours (or as described here).

klickagent added a commit to klickagent/ng-bootstrap that referenced this issue Jul 6, 2019
…to the eventhandler of ngb datepicker directly

As stated in ticket: ng-bootstrap#2181
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants