问题描述:
我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);
}
}
我找不到这里有什么问题。
我在这里重新创建了它。单击该行以展开并查看编辑字段。
尝试定义每个自动完成不同的模板参考,我平均变化#auto="matAutocomplete"
来#location="matAutocomplete"
进行为例,调整[matAutocomplete]="location"
输入。在此处了解有关模板引用的更多信息https://angular.io/guide/template-reference-variables以及如何在 mat 自动完成中使用它们https://material.angular.io/components/autocomplete/overview
此外,我没有循环和更改原始数组,而是重新设计了过滤器函数以始终根据输入的 ngModel 返回一个新数组,您可以在完全正常工作的 stackblitz 中尝试它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句