primeng icon indicating copy to clipboard operation
primeng copied to clipboard

AutoComplete not getting focus on load

Open smtgpt62 opened this issue 9 years ago • 6 comments

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 avatar Feb 09 '17 13:02 smtgpt62

@smtgpt62 , In p-dialog, you already tried to use onAfterShow event?

v1d3rm3 avatar Feb 19 '17 17:02 v1d3rm3

@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

smtgpt62 avatar Feb 21 '17 16:02 smtgpt62

Nice, @smtgpt62. I feel happy to help you. Good projects!

v1d3rm3 avatar Feb 21 '17 17:02 v1d3rm3

@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();
  }

nsksaisaravana avatar Feb 19 '18 06:02 nsksaisaravana

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.

GottaCodeNow avatar Feb 07 '19 04:02 GottaCodeNow

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(); }

policz avatar Apr 22 '19 00:04 policz