简体   繁体   English

NgbDatePicker : 专注于输入框

[英]NgbDatePicker : Focus on Input box

I am not able to focus on the input box (on click / focus ), instead it focuses on the calender popup.我无法专注于input框( click / focus ),而是专注于日历弹出窗口。 When i press tab or click on the ngbDatePicker input box, it opens the calendar , and focus is on the today's date (or whichever selected).当我按 Tab 键或单击ngbDatePicker输入框时,它会打开calendar ,并且焦点是今天的日期(或选择的日期)。 As a result, It doesn't let me type the date by keyboard.结果,它不允许我通过键盘输入日期。

But what i want is to focus the input box, even if the calendar is open, so that i can type the date directly.但是我想要的是聚焦输入框,即使日历是打开的,这样我也可以直接输入日期。

I have tried focusing through javascript, but it is not working.我曾尝试通过 javascript 聚焦,但它不起作用。

<input
    id="dateOfBirthInput"
    type="text"
    class="form-control"
    placeholder="Datepicker"
    name="date_of_birth"
    [(ngModel)]="user.date_of_birth"
    ngbDatepicker
    required
    [minDate] = "minDate"
    #dob="ngbDatepicker"
    (click)="dob.open();"
    (focus)="dob.open();"
/>

I tried opening the datepicker from angular component, but it is not working.我尝试从角度组件打开日期选择器,但它不起作用。

<input
    id="dateOfBirthInput"
    type="text"
    class="form-control"
    placeholder="Datepicker"
    name="date_of_birth"
    [(ngModel)]="user.date_of_birth"
    ngbDatepicker
    required
    [minDate] = "minDate"
    #dob="ngbDatepicker"
    (click)="focusDateOfBirthInput();"
    (focus)="focusDateOfBirthInput();"
/>

//component code
@ViewChild("dob") dob: NgbDatepicker;

focusDateOfBirthInput() {
    console.log("focusDateOfBirthInput");
    document.getElementById("dateOfBirthInput").focus();
    console.log(this.dob);
    this.dob.open()
}

Any help will be greatly appreciated.任何帮助将不胜感激。

I have found a workaround to achieve what you want please try this: 我已经找到一种解决方法来实现您想要的,请尝试以下操作:

  1. Inject renderer using constructor like this and import it as well from @angular/core 使用这样的构造函数注入渲染器,并从@ angular / core导入

    constructor(private renderer: Renderer) 构造函数(私有渲染器:Renderer)

  2. And then update your input element like this: 然后像这样更新您的输入元素:

Explaination: I have taken another input reference named "inputRef" and will take it in component.ts file as ViewChild. 说明:我采用了另一个名为“ inputRef”的输入引用,并将其在component.ts文件中作为ViewChild。

  1. Take the reference of input element via "ViewChild" and implement the method with settimeout 通过“ ViewChild”获取输入元素的引用,并使用settimeout实现该方法

    @ViewChild('inputRef') inputRef; @ViewChild('inputRef')inputRef; constructor(private renderer: Renderer) { } 构造函数(私有渲染器:Renderer){}

    focusOnInput(datePickerRef) { datePickerRef.open(); focusOnInput(datePickerRef){datePickerRef.open(); setTimeout(() => { this.renderer.invokeElementMethod(this.inputRef.nativeElement, 'focus'); }, 10) } setTimeout(()=> {this.renderer.invokeElementMethod(this.inputRef.nativeElement,'focus');},10)}

And bingo, I tried and Tested, It will work. 宾果游戏,我尝试并测试过,它将起作用。

You can do like this : 您可以这样:

 public change(el,dael){ dael.open(); setTimeout(function(){ console.log(el); el.focus() },500); } 
 <input id="dateOfBirthInput" type="text" (click)="change($event.target,dob);" (focus)="change($event.target,dob);" class="form-control" placeholder="Datepicker" name="date_of_birth" [(ngModel)]="user.date_of_birth" ngbDatepicker required [minDate] = "minDate" #dob="ngbDatepicker" /> 

here is a working solution for这是一个有效的解决方案

  • ng-bootstrap 9.0.2 ng-bootstrap 9.0.2
  • Angular 11角 11

Other versions not tested.其他版本未测试。

We need ngZone so that the datepicker popup is not "re-focused" if the date-input is focused.我们需要 ngZone 以便日期选择器弹出窗口不会在日期输入被聚焦时“重新聚焦”。

    // (...)
        
        @ViewChild('datePicker') datePicker: any;
        
       //  (...)
        
        constructor(private ngZone: NgZone) {}
        
        public toggleDatePicker(): void {
            this.datePicker.toggle();
            if (this.datePicker.isOpen()) {

         this.ngZone.onStable.asObservable().pipe(take(1)).subscribe(() => {
                const elementToFocus = this.datePicker._elRef.nativeElement;
                if (elementToFocus) {
                  elementToFocus.focus();
                }
              });
            }
          }
       // in template
     <input type="text"
               [attr.id]="id"
               [attr.aria-describedby]="validationId"
               #datePicker="ngbDatepicker"
               ngbDatepicker
               (click)="toggleDatePicker()" 
               (...)
    />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM