gpt4 book ai didi

angular - 如何停止在 Angular 垫片中添加新值和重复值?

转载 作者:行者123 更新时间:2023-12-05 00:45:31 26 4
gpt4 key购买 nike

我正在使用 angular 9 mat-chips,我想知道如何停止在输入中添加新值,只允许添加自动完成列表中的项目,即输入不在列表中的“abc”自动完成列表并按回车键会在输入中添加“abc 作为芯片”,需要避免只添加自动完成列表中的值。另外,我想知道如何停止在 Angular 垫片中添加重复项,即如果我已经添加了柠檬柠檬,则不应将其添加到垫片列表中,并且也应从自动完成列表中删除。

以下是代码:

chip-autocomplete.component.ts

@Component({
selector: 'chips-autocomplete-example',
templateUrl: 'chips-autocomplete-example.html',
styleUrls: ['chips-autocomplete-example.css'],
})

export class ChipsAutocompleteExample {
visible = true;
selectable = true;
removable = true;
separatorKeysCodes: number[] = [ENTER, 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 {
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);
}
}

chip-autocomplete.component.html

<mat-form-field class="example-chip-list">
<mat-chip-list #chipList aria-label="Fruit selection">
<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"
(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>

app.component.html

<div class="mat-app-background basic-container">
<chips-autocomplete-example>loading</chips-autocomplete-example>
</div>

可以在以下位置找到类似于此代码的堆栈 Blitz (来自 Angular Material Design):https://stackblitz.com/angular/gdjdrkxaedv?file=src%2Fapp%2Fchips-autocomplete-example.ts

最佳答案

您可以添加过滤方法来删除下拉列表中的重复条目。

getUniqueList(fruitList: string[]) {
return fruitList.filter(x => this.fruits.indexOf(x) === -1);
}

以及你的构造函数的所有过滤方法。

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

然后为 remove 方法添加更新

remove(fruit: string): void {
const index = this.fruits.indexOf(fruit);

if (index >= 0) {
this.fruits.splice(index, 1);
}
this.fruitCtrl.updateValueAndValidity();
}

要添加独特的项目,您可以使用以下代码。

// Add our fruit
if ((value || "").trim()) {
const filterList = this.getUniqueList(this.allFruits);
const index = filterList.indexOf(event.value);
if (index > -1) {
this.fruits.push(value.trim());
}
}

您可以引用更新后的代码 here.

关于angular - 如何停止在 Angular 垫片中添加新值和重复值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63345102/

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