【发布时间】:2019-12-06 17:07:14
【问题描述】:
有一些使用 JSON 结构的 MatOptGroup 过滤示例,但它们不是嵌套的。这就是为什么我要解决这个问题。
<mat-form-field>
<mat-label>Database</mat-label>
<mat-select [formControl]="databaseControl">
<mat-option>-- None --</mat-option>
<mat-optgroup *ngFor="let group of databaseGroups" [label]="group.name" [disabled]="group.disabled">
<mat-option *ngFor="let children of group.children" [value]="children.name">
{{children.name}}
</mat-option>
</mat-optgroup>
</mat-select>
</mat-form-field>
目前它是这样工作的。结构嵌套如下:
export interface Database {
name: string;
}
export interface DatabaseGroup {
disabled?: boolean;
name: string;
children: Database[];
}
有一个没有嵌套结构的例子:https://stackblitz.com/edit/angular-smg2xm?file=app/app.component.ts
我尝试过类似的方法,但这不起作用,因为过滤器不知道彼此的值:
private _filterGroups(value: string): string[] {
const filterValue = value.toLowerCase();
this.options = this.databaseGroups.filter(function (database) {
database.children.filter(function (sipp) {
return sipp.name.toLowerCase().includes(filterValue);
})
});
return this.options;
}
没有嵌套结构的解决方案:
filterGroup(val: string): SpeciesGroup[] {
if (val) {
return this.speciesGroup
.map(group => ({ letter: group.letter, name: this._filter(group.name, val) }))
.filter(group => group.name.length > 0);
}
return this.speciesGroup;
}
【问题讨论】:
标签: angular autocomplete