Components: autocomplete panel doesn't pop up when triggering focus on the input programatically

Created on 15 Feb 2017  路  24Comments  路  Source: angular/components

see https://gist.run/?id=19eec6a0d5c69d65dbb847b12009ca9f, it starts working only after you do it the second time

@kara

P3 materiaautocomplete has pr

Most helpful comment

I got it working with the following:

<input (focus)="onFocus()" ... >
@ViewChild(MdAutocompleteTrigger) trigger;

onFocus() {
  this.trigger._onChange(""); 
  this.trigger.openPanel();
}

All 24 comments

I'm not at all familiar with using ngZone, but it seems like this change fixes it

autocomplete.ts


/** Panel should hide itself when the option list is empty. */
_setVisibility() {
  // Promise.resolve().then(() => this.showPanel = !!this.options.length);
  this.zone.run(() => this.showPanel = !!this.options.length);
}

Is there any workaround for this for now?

The issue actually seems to be that clicking a button to focus the autocomplete triggers an _outsideClickStream event just after it has been opened, thus causing it to immediately close.

As a workaround, you can use a setTimeout to focus it after the outside click event fires.

http://plnkr.co/edit/cKYxnL9f8hV2Uklucd5d?p=preview

Alternatively use stopPropagation() on the click event

http://plnkr.co/edit/m4W7zAGulSaxhPqqlpzp?p=preview

Below code did the trick for me assuming focus is already on input field.

import { MdAutocompleteTrigger} from '@angular/material';

@ViewChild(MdAutocompleteTrigger) trigger;

this.trigger._onChange("");
this.trigger.openPanel();

Combined willshowell and bruteforce solutions to solve for our 508 compliance scenario. Clear button is another scenario:
openAutoSearchPanel() {
if (this.searchText.length === 0) {
this.autoSearchInputTrigger._onChange('');
this.autoSearchInputTrigger.openPanel();
}
}
clearSearchText() {
console.log('>>> clearing searchText');
this.searchText = '';
/// hack: Another reported workaround - Allow the ClickStream to finish executing via setTimeout.
/// https://github.com/angular/material2/issues/3106
setTimeout(() => {
this.autoSearchInput.nativeElement.focus();
this.openAutoSearchPanel();
});
}

yeah, got the same issue while implement custom field with autocomplete, timeout works, but this is pretty weird

Any update on this...?

+1, any updates?

This problem also affects chiplist inputs with autocomplete. 馃槩

Is there any workaround or fix for this issue?
I'm having a dropdown that fetches data dynamically from the server. When I click on the chip-list field it doesn't open autocomplete list until I input any character in that.

This problem also affects chiplist inputs with autocomplete. 馃槩

Me too, do you have any workaround?

I got it working with the following:

<input (focus)="onFocus()" ... >
@ViewChild(MdAutocompleteTrigger) trigger;

onFocus() {
  this.trigger._onChange(""); 
  this.trigger.openPanel();
}

Thank you for response. I'll try this solution and will check if this works.

Just for a note that I'm fetching the list from REST API.

Thanks @mischkl
I changed to (click) and it's worked.

Regards,
Huka

Thank you @mischkl 馃憤 that solution worked 馃槃

I got it working with the following:

<input (focus)="onFocus()" ... >
@ViewChild(MdAutocompleteTrigger) trigger;

onFocus() {
  this.trigger._onChange(""); 
  this.trigger.openPanel();
}

great! thanks

@ViewChild(MdAutocompleteTrigger) trigger;

Is there any other part missing from this? It's not working for me :(

Hey, im trying to use the MatAutocompleteTrigger and openPanel, but, the function openPanel is undefined... how can i open the open from trigger?

@lucas-sesti this works for me:

@ViewChild(MatAutocompleteTrigger, { static: true }) trigger: MatAutocompleteTrigger;
// ****
 onClick(){
      this.trigger.openPanel();
} 

import { MatAutocompleteTrigger } from '@angular/material/autocomplete';

Is the above import correct?

Quick fix in case you have more than 1 <mat-autocomplete> in your component:

In your .html

<input (focus)="onFocus(0)" ... >
<input (focus)="onFocus(1)" ... >
<input (focus)="onFocus(2)" ... >

In your .ts

import {ViewChildren, QueryList} from '@angular/core';
import {MatAutocompleteTrigger} from '@angular/material/autocomplete';

@ViewChildren(MatAutocompleteTrigger) autoComplete: QueryList<MatAutocompleteTrigger>;

onFocus(value){
this.autoComplete['_results'][value]._onChange('');
this.autoComplete['_results'][value].openPanel();
}

Still this is a patch, in my case I discovered time after, that the problem was how data arrived after the first filter was executed. So I didn't use this solution anymore.

Here the link where another developer had the same problem.
I hope will help someone:
https://stackoverflow.com/questions/52536382/how-to-show-autocomplete-options-on-focus

Will this ever get fixed?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

3mp3ri0r picture 3mp3ri0r  路  3Comments

shlomiassaf picture shlomiassaf  路  3Comments

kara picture kara  路  3Comments

MurhafSousli picture MurhafSousli  路  3Comments

dzrust picture dzrust  路  3Comments