多个自动完成材质角度显示相同的下拉列表

时间:2018-01-12 09:44:27

标签: angular angular-material

我的多个下拉菜单有效但显示的值相同。我正在关注angular material component

我为每个数组对象创建了不同的过滤器,但仍显示相同的下拉列表。

import { Component } from '@angular/core';
import { FormControl } from '@angular/forms';
import { Observable } from 'rxjs/Observable';
import { startWith } from 'rxjs/operators/startWith';
import { map } from 'rxjs/operators/map';
import { UsersComponent } from '../users/users.component';

@Component({
  selector: 'app-accommodation',
  templateUrl: './accommodation.component.html',
  styleUrls: ['./accommodation.component.css']
})
export class AccommodationComponent {

      myControl = new FormControl();

      country = [
        new UsersComponent('United States'),
        new UsersComponent('Canada'),
        new UsersComponent('Brazil'),
        new UsersComponent('India'),
        new UsersComponent('China'),
        new UsersComponent('Japan'),
      ];

      nationality = [
        new UsersComponent('American'),
        new UsersComponent('Canadian'),
        new UsersComponent('Indian'),
        new UsersComponent('Chinese'),
        new UsersComponent('African'),
        new UsersComponent('Japanese'),
      ];


      countryFilter: Observable<UsersComponent[]>;
      nationalityfilter: Observable<UsersComponent[]>;

      ngOnInit() {
        this.countryFilter = this.myControl.valueChanges
          .pipe(
            startWith<string | UsersComponent>(''),
            map(value => typeof value === 'string' ? value : value.name),
            map(name => name ? this.filter(name) : this.country.slice()),
          );
        this.nationalityfilter = this.myControl.valueChanges
          .pipe(
            startWith<string | UsersComponent>(''),
            map(value => typeof value === 'string' ? value : value.name),
            map(name => name ? this.filter(name) : this.nationality.slice()),
          );
      }


      filter(name: string): UsersComponent[] {

        return this.country.filter(option => option.name.toLowerCase().indexOf(name.toLowerCase()) === 0),
               this.nationality.filter(option => option.name.toLowerCase().indexOf(name.toLowerCase()) === 0);       
      }

      displayFn(users?: UsersComponent): string | undefined {
        return users ? users.name : undefined;
      }

}

下面是HTML代码,我根据this post更改了ID。仍然无法工作。

        <div class="col-sm-6">
        <mat-form-field class="example-full-width">
            <input type="text" placeholder="Country" aria-label="Country" matInput [formControl]="myControl" [matAutocomplete]="auto">
            <mat-autocomplete #auto="matAutocomplete" [displayWith]="displayFn">
                <mat-option *ngFor="let option of countryFilter | async" [value]="option">
                    {{ option.name }}
                </mat-option>
            </mat-autocomplete>
        </mat-form-field>
    </div>
    <!-- col end -->
    <div class="col-sm-6">
        <mat-form-field class="example-full-width">
            <input type="text" placeholder="Nationality" aria-label="Nationality" matInput [formControl]="myControl" [matAutocomplete]="auto">
            <mat-autocomplete #autoNationality="matAutocomplete" [displayWith]="displayFn">
                <mat-option *ngFor="let option of nationalityfilter | async" [value]="option">
                    {{ option.name }}
                </mat-option>
            </mat-autocomplete>
        </mat-form-field>
    </div>
    <!-- col end -->

在@Mjstk更新之后,它工作得很棒,但仍然显示出一些错误 enter image description here

1 个答案:

答案 0 :(得分:6)

您必须更改第二个自动填充的名称,因此它不会链接到第一个自动填充,也不会更改&#34; auto&#34; to&#34; autoNationality&#34;:[matAutocomplete] =&#34; autoNationality&#34;&gt;

<mat-form-field class="example-full-width">
            <input type="text" placeholder="Nationality" aria-label="Nationality" matInput [formControl]="myControl" [matAutocomplete]="autoNationality">
            <mat-autocomplete #autoNationality="matAutocomplete" [displayWith]="displayFn">
                <mat-option *ngFor="let option of nationalityfilter | async" [value]="option">
                    {{ option.name }}
                </mat-option>
            </mat-autocomplete>
        </mat-form-field>