简体   繁体   English

如何在焦点上显示自动完成选项

[英]How to show autocomplete options on focus

I'm writing an angular6 application with latest angular-material.我正在用最新的 angular-material 编写一个 angular6 应用程序。

I use the component mat-autocomplete with a mat-input for an auto-complete feature.我使用带有mat-input的组件mat-autocomplete来实现自动完成功能。

What I'm trying to achieve is that when the user focus on the input element, he will see all the available auto-complete options even without typing anything.我想要实现的是,当用户关注输入元素时,即使不输入任何内容,他也会看到所有可用的自动完成选项。

This is the html file of the mat-autocomplete component:这是 mat-autocomplete 组件的 html 文件:

<form [formGroup]="carTypeFormGroup" (ngSubmit)="okButton()">
  <mat-form-field>
    <input matInput formControlName="carCompany"
           placeholder="foo" aria-label="foo" [matAutocomplete]="autoCarCompany">
    <mat-autocomplete  #autoCarCompany="matAutocomplete">
      <mat-option *ngFor="let carCompany of filteredCarCompanies | async" [value]="carCompany">
        <span>{{carCompany}}</span>
      </mat-option>
    </mat-autocomplete>
  </mat-form-field>
...

And this is the code for the component's class:这是组件类的代码:

@Component({
  selector: 'app-car-type',
  templateUrl: './car-type.component.html',
  styleUrls: ['./car-type.component.scss']
})
export class CarTypeComponent implements OnInit {

  carTypeFormGroup: FormGroup;

  filteredCarCompanies: Observable<CarType[]>;
  filteredCarModels: Observable<CarType[]>;
  carCompanies = [];
  carCompaniesLowercase = [];
  carModels = [];
  carTypes = [];

 private _filterCarCompanies(value: string): CarType[] {
    if (this.carCompaniesLowercase.indexOf(value.toLowerCase()) >= 0) {
      this.mainGql.GetCarModels(value).subscribe((data: any) => {
        this.carModels = [];
        data.data.car_models.forEach((row) => {
          this.carModels.push(row.model_name);
        });
      });
    }
    const filterValue = value.toLowerCase();
    return this.carCompanies.filter(carCompany => carCompany.toLowerCase().indexOf(filterValue) === 0);
  }

ngOnInit() {
    this.carTypeFormGroup = this.formBuilder.group({
      carCompany: ['', Validators.required],
      carModel: ['', Validators.required],
      carType: ['', Validators.required],
      carYear: [new Date().getFullYear(), Validators.required]
    });
    this.filteredCarCompanies = this.carTypeFormGroup.get('carCompany').valueChanges
      .pipe(startWith(''), map(carCompany => carCompany ? this._filterCarCompanies(carCompany) : this.carCompanies.slice()));
}
...
}

When I check the mat-autocomplete examples at https://material.angular.io/components/autocomplete/examples , then when I focus on the input element, I do see all the results..当我在https://material.angular.io/components/autocomplete/examples检查mat-autocomplete示例时,当我专注于输入元素时,我确实看到了所有结果。

What's the difference?有什么不同? What am I missing?我错过了什么?

the filter is executed when the page loads.. but I loaded the data on graphql so the data arrived after the first filter executed.页面加载时执行过滤器..但我在graphql上加载了数据,所以数据在第一个过滤器执行后到达。 I changed it so the filter will be executed only after the data was received.我对其进行了更改,因此仅在收到数据后才会执行过滤器。

thanks Swoox for helping me notice it.感谢 Swoox 帮助我注意到它。

ngOnInit() {
 ...
 this.carsService.GetCarCompanies().subscribe((data: any) => {
      this.carCompanies = [];
      this.carCompaniesLowercase = [];
      data.data.car_companies.forEach((row) => {
        this.carCompanies.push(row.company_name);
        this.carCompaniesLowercase.push(row.company_name.toLowerCase());
      });
      this.filteredCarCompanies = this.carTypeFormGroup.get('carCompany').valueChanges
        .pipe(startWith(''), map(carCompany => carCompany ? this._filterCarCompanies(carCompany) : this.carCompanies.slice()));
    });

There are 2 solutions to it.有2个解决方案。

  1. You can also add the filter inside a set timeout for observables:您还可以在 observables 的设置超时内添加过滤器:
setTimeout(()=>{ 
this.filteredCarCompanies = this.carTypeFormGroup.get('carCompany').valueChanges
        .pipe(startWith(''), map(carCompany => 
        carCompany ? this._filterCarCompanies(carCompany) : this.carCompanies.slice()));
},0);
  1. Add debounceTime(500) :添加debounceTime(500)
this.filteredCarCompanies = this.carTypeFormGroup.get('carCompany').valueChanges
        .pipe(startWith(''),debounceTime(500), map(carCompany => 
        carCompany ? this._filterCarCompanies(carCompany) : this.carCompanies.slice()));

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

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