Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set focus for AutoComplete component from PrimeNG library in Angular

I am trying to use a AutoCompete component inside a Dialog element. I want the focus goes to the AutoComplement element when the Diaglog opens.

I have not find a exact tutorial on this.

My effect is based on this stackoverflow link: How to use Angular4 to set focus by element id

And this Github issue: https://github.com/primefaces/primeng/issues/2029 Although I do not understand some part like the onAfterShow event, and some guy in that thread tried and does not work.

My code goes like this(simplified):

Component

<p-dialog [(visible)]="this.display" modal="true"(onShow)="this.onAfterShow($event)">
  <p-autoComplete  #autoCompleteObject>
  </p-autoComplete>
  <some-other-components>
<p-dialog>

TypeScript:

  @ViewChild('autoCompleteObject') private autoCompleteObject: AutoComplete ;
  onAfterShow(event){   this.autoCompleteObject.domHandler.findSingle(this.autoCompleteObject.el.nativeElement, 'input').focus();
  }

or like that:

@ViewChild('autoCompleteObject', {read: ElementRef}) private autoCompleteObject: ElementRef ;
  onAfterShow(event){
    console.log("diaglog shows");
    this.autoCompleteObject.nativeElement.focus();
  }

When when the diaglog opens, the onAfterShow() function is executed without error. However, the focus is not set on the autocomplete element.

Any suggestions where I got it wrong? Thank you in advance.

like image 304
Haijin Avatar asked Oct 01 '18 22:10

Haijin


People also ask

What is autocomplete component in angular?

Angular Autocomplete Component Autocomplete is a User interface feature and it contains an Input text box and allows the developer to type text, and application search and display the matched words results for typed characters and give complete word prediction list. This is helpfull for User to search and select instead of typing in text box.

How to set default focus on input box in angular?

Make It Easy: Angular auto focus for input box - Angular. 1 1. Auto focus on textbox using directive. Especially while dealing with forms you may need to set default focus on some fields. We are going to create ... 2 2. Set focus on text box on click. 3 3. Auto focus on textbox without using a directive.

How to enable autocomplete in primeng using ngmodule?

Primeng provides autocomplete feature via AutoCompleteModule . So, you have to import this module into your application module. app.module.ts file. Import autocomplete module and configure Imports section of NgModule configuration In Application Component, Need to provide data as well as method or function that checks against typed keyword

What is primeng in Angular 4?

PrimeNG from PrimeFaces is an alternative UI component library. It offers a selection of pre-built themes and UI components for data presentation, form inputs, menus, charts, overlays, and more. In this article, you will set up PrimeNG in an Angular 4+ project and explore some of the main components.


1 Answers

Each autocomplete instance has a public function focusInput(). Once you have a reference to your instance via @ViewChild('autoCompleteObject')..., you can call focusInput() on this reference:

onAfterShow(event) {
  this.autoCompleteObject.focusInput();
}

STACKBLITZ

UPDATE

This is related to primeng v1.0.xxx: In order to manually control focus within dialog add [focusOnShow]="false":

<p-dialog header="Title" 
          [focusOnShow]="false" 
          [(visible)]="display" 
          modal="true" 
          (onShow)="onAfterShow()">
   ...
</p-dialog>

this way, the button does not "steal" the focus, STACKBLITZ updated

like image 157
Andriy Avatar answered Oct 13 '22 02:10

Andriy