AutoComplete not getting focus on load
Hi,
I am using p-autocomplete inside p-dialog . How can I set focus to autocomplete when the dialog opens.
Please help on this issue.
@smtgpt62 , In p-dialog, you already tried to use onAfterShow event?
@victorschinaider Thanks for help, The issue is solved by using the following code in onAfterShow event
this.autoCompleteObject.domHandler.findSingle(this.autoCompleteObject.el.nativeElement, 'input').focus();
where this.autoCompleteObject is the reference of
Nice, @smtgpt62. I feel happy to help you. Good projects!
@smtgpt62 I followed as per your instruction but the below code is not working
<p-autoComplete #autoCompleteObject id="autoCompleteObject" [(ngModel)]="selectedCompany" (onSelect)="onSearchSelected()" [suggestions]="companyResult" (completeMethod)="searchCompany($event)" ></p-autoComplete>
@ViewChild('autoCompleteObject') autoCompleteObject: AutoComplete ;
onAfterShow(){
this.autoCompleteObject.domHandler.findSingle(this.autoCompleteObject.el.nativeElement, 'input').focus();
}
The p-dialog needs the "focusOnShow" property set to false or it will interfere:
[focusOnShow]="false"
Also, I'm guessing the event name changed somewhere in the recent past, because rather than "onAfterShow," the documented event "onShow" is what worked for me.
In addition, instead of the line: this.autoCompleteObject.domHandler.findSingle(this.autoCompleteObject.el.nativeElement, 'input').focus();
You can just do this: this.autoCompleteObject.focusInput();
Both lines work, but the second is simpler.
Hello i'm using p-autocomplete element tag ,which is not inside of p-dialog box,
i'm trying to implement autofocus to a particular area or element upon opening a model window i have tried with different ways of implementation but nothing gets solve my issue.
From my investigation, when the onAutoShow() function triggers just (this.resourceSignum) gets undefined
Below is my code of html and component.
.html file
<p-autoComplete #resourceSignum (onShow)= "onAfterShow()" [suggestions]="resourceSuggestions" (completeMethod)="searchRecoveries($event)" formControlName="signum">
component.ts file
import { AutoComplete } from 'primeng/autocomplete';
@ViewChild('resourceSignum') private resourceSignum: AutoComplete;
onAfterShow() { this.resourceSignum.focusInput(); }