ngbDatepicker not working when enter button it pressed

寵の児 提交于 2021-02-10 19:08:30

问题


I am using ngbDatepicker and it works only if I select the date from the dropdown. However if I change the date via keyboard and press enter nothing happens.

<div class="input-group">
                            <input name="datepicker"
                                   class="form-control"
                                   ngbDatepicker
                                   #datepicker="ngbDatepicker"
                                   [autoClose]="'outside'"
                                   (dateSelect)="onDateSelection($event)"
                                   [displayMonths]="2"
                                   [dayTemplate]="t"
                                   outsideDays="hidden"
                                   [startDate]="fromDate">
                            <ng-template #t let-date let-focused="focused">
                                <span class="custom-day"
                                      [class.focused]="focused"
                                      [class.range]="isRange(date)"
                                      [class.faded]="isHovered(date) || isInside(date)"
                                      (mouseenter)="hoveredDate = date"
                                      (mouseleave)="hoveredDate = null">
                                  {{ date.day }}
                                </span>
                            </ng-template>

in the component:

isHovered(date: NgbDate) {
        return this.fromDate && !this.toDate && this.hoveredDate && date.after(this.fromDate) && date.before(this.hoveredDate);
    }

    isInside(date: NgbDate) {
        return date.after(this.fromDate) && date.before(this.toDate);
    }

    isRange(date: NgbDate) {
        return date.equals(this.fromDate) || date.equals(this.toDate) || this.isInside(date) || this.isHovered(date);
    }

    validateInput(currentValue: NgbDate, input: string): NgbDate {
        const parsed = this.formatter.parse(input);
        return parsed && this.calendar.isValid(NgbDate.from(parsed)) ? NgbDate.from(parsed) : currentValue;
    }

in component:

onDateSelection(date: NgbDate) {
        if (!this.fromDate && !this.toDate) {
            this.fromDate = date;
            this.emitFromDate();
        } else if (this.fromDate && !this.toDate && date.after(this.fromDate)) {
            this.toDate = date;
            this.emitToDate();
        } else {
            this.toDate = null;
            this.fromDate = date;
            this.emitFromDate();
        }
    }

Has anyone encountered this and was able to get it to work?


回答1:


just use a ViewChild to datePicker and close in the function onDateSelection

@ViewChild('datepicker',{static:false}) ngbDatepicker

onDateSelection(date: NgbDate) {
    if (!this.fromDate && !this.toDate) {
      this.fromDate = date;
    } else if (this.fromDate && !this.toDate && date.after(this.fromDate)) {
      this.toDate = date;
      this.ngbDatepicker.close(); //<---this one

    } else {
      this.toDate = null;
      this.fromDate = date;
    }
  }

See in stackblitz




回答2:


Input elements from ngx-bootstrap are created dynamically and don't seem to provide an interface to interact with them.

As a work around, you can retrieve a reference on these inputs (in ngAfterViewInit) and manyally bind a listener to key events, like below

component.ts

ngAfterViewInit()
{
    let dpFrom = document.querySelector('input[name=dpFromDate]');
    this.renderer.listen(dpFrom,"keydown", (evt: KeyboardEvent)=>
    {
      if(evt.key=='Enter')
      {
        console.log('From date selected', this.fromDate);
      }

    });

    let dpTo = document.querySelector('input[name=dpToDate]');
    this.renderer.listen(dpTo,"keydown", (evt: KeyboardEvent)=>
    {
      if(evt.key=='Enter')
      {
        console.log('To date selected', this.toDate);
      }

    })

}

Here is a stackblitz example



来源:https://stackoverflow.com/questions/60771387/ngbdatepicker-not-working-when-enter-button-it-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!