当我们通过箭头键进入选项并按空格键(32)选择mat-chip
时如何填充onkeypress(spacebar)
mat-option
。
但是,当我们选择选择下拉菜单时,可以通过箭头键进入选项,然后按Enter键(键码13),但不能在空格键(键码-32)上进行类似操作,效果很好。
在这里,是stackblitz链接:- https://stackblitz.com/edit/angular-ytk8qk-feaqaw?file=app/chips-autocomplete-example.html
1) How to add select dropdown option by going through
arrowkey(not mouse) and populating selected option using spacebar(keycode- 32).
2)How to remove option from dropdown that is already populated or used.
3)Show dropdown only when user enters some charcter in input text else show
class="info"` text only in dropdown, when no input text is there and no
option in dropdown matches enter charcters in input.
Note:- The user can create chips by typing in input and then press ENTER or SPACE key (separator key) for creating chips.
export class ChipsAutocompleteExample {
visible = true;
selectable = true;
removable = true;
addOnBlur = true;
separatorKeysCodes: number[] = [ENTER,SPACE, COMMA];
fruitCtrl = new FormControl();
filteredFruits: Observable<string[]>;
fruits: string[] = ['Lemon'];
allFruits: string[] = ['Apple', 'Lemon', 'Lime', 'Orange', 'Strawberry'];
@ViewChild('fruitInput') fruitInput: ElementRef<HTMLInputElement>;
@ViewChild('auto') matAutocomplete: MatAutocomplete;
constructor() {
this.filteredFruits = this.fruitCtrl.valueChanges.pipe(
startWith(null),
map((fruit: string | null) => fruit ? this._filter(fruit) : this.allFruits.slice()));
}
add(event: MatChipInputEvent): void {
// Add fruit only when MatAutocomplete is not open
// To make sure this does not conflict with OptionSelected Event
if (!this.matAutocomplete.isOpen) {
const input = event.input;
const value = event.value;
// Add our fruit
if ((value || '').trim()) {
this.fruits.push(value.trim());
}
// Reset the input value
if (input) {
input.value = '';
}
this.fruitCtrl.setValue(null);
}
}
remove(fruit: string): void {
const index = this.fruits.indexOf(fruit);
if (index >= 0) {
this.fruits.splice(index, 1);
}
}
selected(event: MatAutocompleteSelectedEvent): void {
this.fruits.push(event.option.viewValue);
this.fruitInput.nativeElement.value = '';
this.fruitCtrl.setValue(null);
}
private _filter(value: string): string[] {
const filterValue = value.toLowerCase();
return this.allFruits.filter(fruit => fruit.toLowerCase().indexOf(filterValue) === 0);
}
}
答案 0 :(得分:2)
1)如何通过执行添加选择下拉选项 箭头键(不是鼠标),并使用空格键(键码32)填充选定的选项。
添加属性以保存选定的水果和当前显示的水果(已过滤的水果):
selectedFruit = -1;
displayedFruits = [];
在查看初始化之后,订阅keyManager上的更改以获取所选选项,并订阅已过滤水果的更改以获取已过滤列表并将其存储在displayFruits上:
ngAfterViewInit() {
this.matAutocomplete._keyManager.change.subscribe((index) => {
if (index >= 0) {
this.selectedFruit = index;
}
})
this.filteredFruits.subscribe((filteredFruits) => {
this.displayedFruits = filteredFruits;
});
}
在add方法上,包含一个else子句以包含水果,并将selectedFruit重置为-1:
add(event: MatChipInputEvent): void {
// Add fruit only when MatAutocomplete is not open
// To make sure this does not conflict with OptionSelected Event
if (!this.matAutocomplete.isOpen) {
// ...
} else {
if (this.selectedFruit >= 0) {
this.fruits.push(this.displayedFruits[this.selectedFruit])
this.fruitInput.nativeElement.value = '';
this.fruitCtrl.setValue(null);
} else if (this.fruitInput.nativeElement.value !== '' && this.displayedFruits.length === 0) {
this.fruits.push(this.fruitInput.nativeElement.value)
this.fruitInput.nativeElement.value = '';
this.fruitCtrl.setValue(null);
}
}
this.selectedFruit = -1;
}
2)如何从已填充或已使用的下拉菜单中删除选项。
增强过滤器以检查是否已经使用过的水果:
private _filter(value: string): string[] {
const filterValue = value.toLowerCase();
return this.allFruits.filter(fruit => fruit.toLowerCase().indexOf(filterValue) === 0 && !this.fruits.find( existingFruit => existingFruit === fruit ));
}
3)仅在用户在输入文本中输入字符时显示下拉菜单,否则显示 当没有输入文本且没有输入文本时,仅在下拉列表中显示class =“ info”`文本 下拉菜单中的选项在输入中输入字符。
如果我做对了,您可以这样做:
绑定输入焦点事件以在输入焦点对准时显示自动完成
<input
placeholder="New fruit..."
#fruitInput
(focus)="matAutocomplete.showPanel = true"
[formControl]="fruitCtrl"
[matAutocomplete]="auto"
[matChipInputFor]="chipList"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes"
[matChipInputAddOnBlur]="addOnBlur"
(matChipInputTokenEnd)="add($event)">
修改自动完成模板,以在没有输入文本或没有值匹配时显示额外的class =“ info”选项:
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
<mat-option class="info" *ngIf="displayedFruits.length === 0 || fruitInput.value === ''" disabled>Test</mat-option>
<ng-container *ngIf="fruitInput.value !== ''">
<mat-option *ngFor="let fruit of displayedFruits" [value]="fruit">
{{fruit}}
</mat-option>
</ng-container>
</mat-autocomplete>
正在工作的堆叠here