mat-autocomplete 在 mat-table 中不起作用

基兰

问题描述:

mat-autocomplete用几乎相同的代码定义了多个,如下所示,以从多个列表中获取数据。

<div class="col-md-4">
<mat-form-field class="example-full-width">
  <mat-label>Level</mat-label>
  <input matInput name="level" aria-label="Level" [matAutocomplete]="auto" 
  [(ngModel)]="element.Level" (ngModelChange)="filtredSelection($event,'Levels')">
  <mat-autocomplete #auto="matAutocomplete" [displayWith]="displaySelectedLevel" >
    <mat-option *ngFor="let level of filtredLevels | async" [value] = "element.Level">
      <div (click)="levelsOptionClicked($event, element, level)">
        <span>{{level.Code}}</span> |
        <small>{{level.Description}}</small>
      </div>
    </mat-option>
  </mat-autocomplete>
</mat-form-field>
</div>
<div class="col-md-8">
<mat-form-field class="example-full-width">
  <mat-label>Location</mat-label>
  <input matInput name="location" aria-label="Location" [matAutocomplete]="auto" 
  [(ngModel)]="element.Location" (ngModelChange)="filtredSelection($event,'Locations')">
  <mat-autocomplete #auto="matAutocomplete" [displayWith]="displaySelectedLocation" >
    <mat-option *ngFor="let location of filtredLocations | async" [value] = "element.Location">
      <div (click)="locationsOptionClicked($event, element, location)">
        <span>{{location.Code}}</span> |
        <small>{{location.Description}}</small>
      </div>
    </mat-option>
  </mat-autocomplete>
</mat-form-field>
</div>

第一个有效,并显示选择列表,但是在第二个中,我可以看到它正在拨打电话并获得结果,但下拉选择没有正确显示。

我注意到的奇怪问题是,当我单击并过滤第一个控件上的项目然后移动到第二个控件时,它会在第二个控件的下拉列表中显示第一个控件的选项。

这是我的控制逻辑

  applyLevelSelectionFilter(filter: string) {
    if (filter?.length >= 3) {
     const promise = this.dataService.getAllLevels(filter).then<Level[]>(
        data => {
          this.isLoadingFilters = false;
          let result = [];
          if (data) {
            const levels = (data as any).value;
            this.logger.log('fetched filtred levels', levels);
            result = (data as any).value;
          }
          return result;

        });
        this.filtredLevels = from(promise);
    } else {
      this.logger.log('search string less than 3 char, clearing the level selection', filter);
      return of([]);
    }
  }

  applyLocationSelectionFilter(filter: string) {
    if (filter?.length >= 3) {
      this.isLoadingFilters = true;
      const promise = this.dataService.getAllLocations(filter).then<Location[]>(
        data => {
          let result = [];
          if (data) {
            const locations = (data as any).value;
            this.logger.log('fetched filtred locations', locations);
            result = (data as any).value;
          }
          return result;

        });
        this.filtredLocations = from(promise);
    } else {
      this.logger.log('search string less than 3 char, clearing the location selection', filter);
      return of([]);
    }
  }

  filtredSelection(filter, entitySet){
    this.logger.log('Triggring filtered list update', filter, entitySet);
    if (entitySet === 'Levels' ) {
      this.applyLevelSelectionFilter(filter);
    } else if (entitySet === 'Locations') {
      this.applyLocationSelectionFilter(filter);
    } else {
      this.logger.error('Triggring filtered list update with unknown entity set', filter, entitySet);
    }
  }

我找不到这里有什么问题。

更新 1

在这里重新创建了它单击该行以展开并查看编辑字段。

队长权利

尝试定义每个自动完成不同的模板参考,我平均变化#auto="matAutocomplete" #location="matAutocomplete" 进行为例,调整[matAutocomplete]="location"输入。在此处了解有关模板引用的更多信息https://angular.io/guide/template-reference-variables以及如何在 mat 自动完成中使用它们https://material.angular.io/components/autocomplete/overview

此外,我没有循环和更改原始数组,而是重新设计了过滤器函数以始终根据输入的 ngModel 返回一个新数组,您可以在完全正常工作的 stackblitz 中尝试它。

您可以在从您的 stackblitz 分叉的 stackblitz 上找到更正

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

mat-autoComplete中的占位符不起作用,如Angular Material Documentation中所示

mat-selection-list 不显示在 mat-autocomplete 中

Angular 6-容器-Mat-table中的模板不起作用

mat-table中FormGroup的动态验证FormArray不起作用

FormGroup CustomFilter中的Mat-AutoComplete

提交 mat-autocomplete 的值

Mat到Byte []的转换在Java中不起作用

选中的属性在Mat单选按钮中不起作用

多个 Mat Paginator 在 Angular 组件中不起作用

Angular 7的mat-table中的mat-checkbox和mat-checkbox标头问题

在mat-table Angular中遍历dataSource

Mat Table:如何在Mat Table Angle中单击按钮以获取特定行中的特定元素?

如何在扩展的Mat-row中而不是在Main table中显示mat-table

Mat-autocomplete-如何访问所选选项?

Mat-autocomplete 既不绑定也不验证

如何获得 Mat Autocomplete 的 Panel ElementRef

mat-autocomplete classList Input 如何工作?

Angular Material 2 Table Mat Row Click事件,也称为在Mat Cell中单击按钮

样式“ mat-sort-header-sorted”在mat-table教程中无效吗?

一旦在Angular 8中选择了Mat-Table,就禁用Mat-Select选项

来自 Array 的 Opencv (JavaScript) Mat 不起作用

ngx-mat-select-search不起作用

用数据初始化cv :: Mat不起作用

带selectionModel的mat-checkbox-检查的属性不起作用

Angular 8 - *ngIf 和 mat-checkbox 不起作用

将OpenGL纹理转换为OpenCV Mat不起作用

ngFor中的mat-menu在生产版本中不起作用(Angular 4)

使用openCV(.release()和= Mat()发布Mat图像不起作用)

内置的 deselectAll 方法在多个 Angular mat-selection-list 中不起作用