【发布时间】:2022-01-31 17:01:36
【问题描述】:
在我的代码中,我试图从列表中过滤元素,并且在我一次写多个单词时尝试过滤该元素。现在,如果我按正确的顺序写单词,我可以过滤元素。例如;如果我写'red book',我可以过滤'red book love' 元素,但我也想在写'red love' 时过滤它。这是我的代码,我应该怎么做才能实现我想要的?
HTML:
<mat-form-field appearance="outline" fxFlex="100" class="w-100-p pr-8">
<mat-label>Product</mat-label>
<input type="text" required matInput [(ngModel)]="product" name="product"
(input)="onProductSearchChange($event.target.value)" [matAutocomplete]="autoProductId">
<button mat-button matSuffix mat-icon-button aria-label="Clear" (click)="product = null" type="button">
<mat-icon>block</mat-icon>
</button>
<mat-autocomplete autoActiveFirstOption #autoProductId="matAutocomplete"
[displayWith]="displayProduct.bind(this)" (optionSelected)="filterProduct($event.option.value)">
<mat-option *ngFor="let prm of productList" [value]="prm">
{{prm?.StockIntegrationCode +' '+ prm?.ProductName}}
</mat-option>
</mat-autocomplete>
</mat-form-field>
TS:
onProductSearchChange(search: string) {
let filterValue = (search as any).toLocaleLowerCase("tr");
if (
this._adminService.products &&
this._adminService.products.length > 0
) {
this.productList = this._adminService.products.filter(
(x) =>
x.StockIntegrationCode.indexOf(filterValue) >= 0 ||
(x.ProductName as any)
.toLocaleLowerCase("tr")
.indexOf(filterValue) >= 0
);
}
if (!search) {
this.product = null;
this.productList = this._adminService.products;
}
}
displayProduct(product: IProduct): string | undefined {
return product
? product.StockIntegrationCode + " - " + product.ProductName
: "";
}
【问题讨论】:
-
这应该为您指明正确的方向:stackoverflow.com/questions/55916979/…
标签: javascript html angular typescript