Material-Angular-Select

Material Angular Mat-tab 使用技巧

mat-select 添加输入过滤框

1
2
3
4
<mat-select formControlName="markingCode" (openedChange)="inputField.value = '';selectedCodeList = codeList;">
<input autofocus #inputField (keyup)="onKey($event, codeList, 'selectedCodeList')" placeholder="搜索" />
<mat-option *ngFor="let item of selectedCodeList" [value]="item.key">{{item.name}}</mat-option>
</mat-select>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class AComponent {
codeList = [{}];
constructor() {
this.selectedCodeList = [...this.codeList];
}

// @Params
// originList: T[] 原始数据列表;
// temporaryListString: string 临时列表名称;
// filterField: keyof T 需要过滤的字段(显示什么字段,默认过滤name);
onKey<T extends Record<string, string>>($event: any, originList: T[], temporaryListString: keyof AComponent,filterField: keyof T = 'name'): void {
this[temporaryListString] = originList.filter((option: T) => (option[filterField] || '').toLowerCase().includes($event.target.value.toLowerCase())) as never;
}
}

本文作者: 孟 虎
本文链接: https://menghu1994.github.io/blog/2024/03/Angular/Material-Angular/mat-select/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!