如何使用选项组重置 Material 自动完成中的自定义过滤器
How to reset the Custom Filter in Material Autocomplete with groups of options
如何在 material 自动完成 autocomplete. I have select input with the group of option like here 中重置自定义过滤器值。现在我想创建一个自定义过滤器来过滤我们的结果及其选项组名称。就像在我的 ts 文件中我有:
pokemonGroups = [
{
name: 'Grass',
pokemon: [
'bulbasaur-0', 'Bulbasaur', 'oddish-1','Oddish','bellsprout-2', 'Bellsprout'
]
},
{
name: 'Water',
pokemon: [
'squirtle-3', 'Squirtle', 'psyduck-4','Psyduck', 'horsea-5', 'Horsea'
]
}]
现在在我的 html:
<form [formGroup]="searchForm">
<mat-form-field class="example-full-width">
<input type="text" placeholder="Pokemon" aria-label="Pokemon" matInput formControlName="pokemonControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-optgroup *ngFor="let group of pokemonGroups" [label]="group.name" [disabled]="group.disabled">
<mat-option *ngFor="let poke of group.pokemon" [value]="poke">
{{ poke }}
</mat-option>
</mat-optgroup>
</mat-autocomplete>
</mat-form-field>
</form>
我的打字稿文件:
ngOnInit() {
// ... code
this.searchForm.controls.pokemonControl.valueChanges
.subscribe(
(val) => {
this.filter(val);
}
);
}
filter(val) {
for (const pokemon of this.pokemonGroups) {
pokemon.pokemon= pokemon.pokemon.filter(pok=>
pok.toLowerCase().indexOf(val.toLowerCase()) === 0);
}
return this.pokemonGroups;
}
过滤器工作正常,但是当我按退格键或删除关键字时,它应该会重置我的所有值。这是行不通的。
我没测试过不过你可以这样试试。另请检查 this
ngOnInit() {
this.pokemonControl.valueChanges
.subscribe(
(val) => {
this.filter(val);
}
);
}
filter(val) {
for (const pokemon of this.pokemonGroups) {
pokemon.pokemon= pokemon.pokemon.filter(pokemon=>
pokemon.toLowerCase().indexOf(val.toLowerCase()) === 0);
}
return this.pokemonGroup;
}
如何在 material 自动完成 autocomplete. I have select input with the group of option like here 中重置自定义过滤器值。现在我想创建一个自定义过滤器来过滤我们的结果及其选项组名称。就像在我的 ts 文件中我有:
pokemonGroups = [
{
name: 'Grass',
pokemon: [
'bulbasaur-0', 'Bulbasaur', 'oddish-1','Oddish','bellsprout-2', 'Bellsprout'
]
},
{
name: 'Water',
pokemon: [
'squirtle-3', 'Squirtle', 'psyduck-4','Psyduck', 'horsea-5', 'Horsea'
]
}]
现在在我的 html:
<form [formGroup]="searchForm">
<mat-form-field class="example-full-width">
<input type="text" placeholder="Pokemon" aria-label="Pokemon" matInput formControlName="pokemonControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-optgroup *ngFor="let group of pokemonGroups" [label]="group.name" [disabled]="group.disabled">
<mat-option *ngFor="let poke of group.pokemon" [value]="poke">
{{ poke }}
</mat-option>
</mat-optgroup>
</mat-autocomplete>
</mat-form-field>
</form>
我的打字稿文件:
ngOnInit() {
// ... code
this.searchForm.controls.pokemonControl.valueChanges
.subscribe(
(val) => {
this.filter(val);
}
);
}
filter(val) {
for (const pokemon of this.pokemonGroups) {
pokemon.pokemon= pokemon.pokemon.filter(pok=>
pok.toLowerCase().indexOf(val.toLowerCase()) === 0);
}
return this.pokemonGroups;
}
过滤器工作正常,但是当我按退格键或删除关键字时,它应该会重置我的所有值。这是行不通的。
我没测试过不过你可以这样试试。另请检查 this
ngOnInit() {
this.pokemonControl.valueChanges
.subscribe(
(val) => {
this.filter(val);
}
);
}
filter(val) {
for (const pokemon of this.pokemonGroups) {
pokemon.pokemon= pokemon.pokemon.filter(pokemon=>
pokemon.toLowerCase().indexOf(val.toLowerCase()) === 0);
}
return this.pokemonGroup;
}