【问题标题】:mat-autocomplete Display name but get ID Valuemat-autocomplete 显示名称但获取 ID 值
【发布时间】:2020-04-24 19:38:40
【问题描述】:

我正在使用自动完成(Mat-autocomplete),目前它允许加载“用户”列表。

在自动完成中,我想显示用户名,但我想保存所选用户 ID 的值。

我有这个:

 this.allFruits = val.map(user => user.username);

我更改了这个以获取所有字段,但是我无法获取所选用户 ID 的值,有人可以帮助我吗?

 this.allFruits = val;

我的代码DEMO

HTML

<mat-form-field class="example-chip-list">
  <mat-chip-list #chipList>
    <mat-chip
      *ngFor="let fruit of fruits"
      [selectable]="selectable"
      [removable]="removable"
      (removed)="remove(fruit)">
      {{fruit}}
      <mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
    </mat-chip>
    <input
      placeholder="New fruit..."
      #fruitInput
      [formControl]="fruitCtrl"
      [matAutocomplete]="auto"
      [matChipInputFor]="chipList"
      [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
      [matChipInputAddOnBlur]="addOnBlur"
      (matChipInputTokenEnd)="add($event)">
  </mat-chip-list>
  <mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
    <mat-option *ngFor="let fruit of filteredFruits | async" [value]="fruit">
      {{fruit}}
    </mat-option>
  </mat-autocomplete>
</mat-form-field>

组件

 constructor(private userService: UserService) {
    this.filteredFruits = this.fruitCtrl.valueChanges.pipe(
        startWith(null),
        map((fruit: string | null) => fruit ? this._filter(fruit) : this.allFruits.slice()));
  }

  ngOnInit() {
    this.userService.getUsers().subscribe(
      (val: any[]) =>{
        this.allFruits = val.map(user => user.username);
        this.fruitCtrl.setValue(null); //use this to apply changes instantly
      } 
    )
  }

  remove(fruit: string): void {
    const index = this.fruits.indexOf(fruit);

    if (index >= 0) {
      this.fruits.splice(index, 1);
    }
  }

  selected(event: MatAutocompleteSelectedEvent): void {
    this.fruits.push(event.option.viewValue);
    this.fruitInput.nativeElement.value = '';
    this.fruitCtrl.setValue(null);
  }

  private _filter(value: string): string[] {
    const filterValue = value.toLowerCase();

    return this.allFruits.filter(fruit => fruit.toLowerCase().indexOf(filterValue) === 0);
  }

【问题讨论】:

  • 你想什么时候获取id

标签: angular typescript


【解决方案1】:

也许在上述场景中使用 Hashmap 可能是个好主意

在您的类变量中创建一个新地图,如下所示

allFruits: string[] = [];
nameIdMap =  new Map<string, number>();

现在在初始化 allFruits 时,连同它一起初始化地图...

 this.allFruits = val.map(user => {
      this.nameIdMap.set(user.username, user.id);
      return user.username
    });

您可以使用 map.get 在如下选定的函数中轻松获取此值

selected(event: MatAutocompleteSelectedEvent): void {
  console.log(this.nameIdMap.get(event.option.viewValue));
  this.fruits.push(event.option.viewValue);
  this.fruitInput.nativeElement.value = '';
  this.fruitCtrl.setValue(null);
}

您不需要编写太多额外的代码,也不需要更改任何预运行代码。所以我认为这会很好。

【讨论】:

    猜你喜欢
    • 2020-03-29
    • 2014-05-19
    • 2019-11-30
    • 2019-02-07
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多