gpt4 book ai didi

angular - 有没有办法在自动完成(Angular 4)中进行多项选择?

转载 作者:太空狗 更新时间:2023-10-29 17:06:31 25 4
gpt4 key购买 nike

我想通过自动完成对筛选的项目进行多项选择。灵感来自以下 tutorial我试过这段代码:

组件:

 <form class="example-form">
<mat-form-field class="example-full-width">
<input type="text" placeholder="Pick one" aria-label="Number" matInput [formControl]="myControl" [matAutocomplete]="auto">
<mat-autocomplete #auto="matAutocomplete">
<mat-option *ngFor="let option of filteredOptions | async" [value]="option" multiple>
<mat-checkbox>
{{ option }}
</mat-checkbox>
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>

我添加了标签以启用选择,但它不起作用。一旦我过滤并选择一个选项,菜单就会关闭,甚至不会选中该复选框。 有没有办法在自动完成中进行多选?谢谢!!

最佳答案

Angular Material documentation for chips包括一个很好的示例,说明如何开始使用多项选择自动完成:

<mat-form-field class="example-chip-list">
<mat-chip-list #chipList>
<mat-chip
*ngFor="let fruit of fruits"
[selectable]="selectable"
[removable]="removable"
(removed)="remove(fruit)">
{{fruit}}
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
</mat-chip>
<input
placeholder="New fruit..."
#fruitInput
[formControl]="fruitCtrl"
[matAutocomplete]="auto"
[matChipInputFor]="chipList"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes"
[matChipInputAddOnBlur]="addOnBlur"
(matChipInputTokenEnd)="add($event)">
</mat-chip-list>
<mat-autocomplete #auto="matAutocomplete" (optionSelected)="selected($event)">
<mat-option *ngFor="let fruit of filteredFruits | async" [value]="fruit">
{{fruit}}
</mat-option>
</mat-autocomplete>
</mat-form-field>

基本上,您有一个绑定(bind)到列表的芯片列表和一个允许在自动完成列表中搜索的文本输入。

import {COMMA, ENTER} from '@angular/cdk/keycodes';
import {Component, ElementRef, ViewChild} from '@angular/core';
import {FormControl} from '@angular/forms';
import {MatAutocompleteSelectedEvent, MatChipInputEvent} from '@angular/material';
import {Observable} from 'rxjs';
import {map, startWith} from 'rxjs/operators';

/**
* @title Chips Autocomplete
*/
@Component({
selector: 'chips-autocomplete-example',
templateUrl: 'chips-autocomplete-example.html',
styleUrls: ['chips-autocomplete-example.css'],
})
export class ChipsAutocompleteExample {
visible = true;
selectable = true;
removable = true;
addOnBlur = false;
separatorKeysCodes: number[] = [ENTER, COMMA];
fruitCtrl = new FormControl();
filteredFruits: Observable<string[]>;
fruits: string[] = ['Lemon'];
allFruits: string[] = ['Apple', 'Lemon', 'Lime', 'Orange', 'Strawberry'];

@ViewChild('fruitInput') fruitInput: ElementRef;

constructor() {
this.filteredFruits = this.fruitCtrl.valueChanges.pipe(
startWith(null),
map((fruit: string | null) => fruit ? this._filter(fruit) : this.allFruits.slice()));
}

add(event: MatChipInputEvent): void {
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);
}
}

代码非常简单(根据每个键入字符后的文本输入进行过滤,从列表中删除一个项目等)。当然,这应该根据您的需要进行调整(例如,一旦您选择了一个项目,它应该被过滤掉以用于下一个自动完成结果)。

关于angular - 有没有办法在自动完成(Angular 4)中进行多项选择?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49131094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com