我在我的项目中使用Angular 5。我能够从搜索框中过滤单个值,但我想要实现的是从逗号分隔的搜索框中过滤多个值。
任何帮助将不胜感激。
export class FilterPersonPipe implements PipeTransform {
transform(items: any[], value: string, label:string): any[] {
if (!items) return [];
if (!value) return items;
if (value == '' || value == null) return [];
//value = value.split(',');
var filterVal = value.split(",");
return items.filter(e => e[label].toLowerCase().indexOf(filterVal) > -1);}
<div class="form-group nav nav-pills flex-column " id="div-testFilter">
<form class="form-inline mt-2 mt-md-0 pull-right span3" id="form-testFilter">
<input [(ngModel)]="searchText" name="text" id="input-testFilter"
class="form-control mr-sm-2" type="text" placeholder="filter Person"
aria-label="Search" >
</form>
</div>
<div>
<table>
<tr><th>Name</th></tr>
<tr *ngFor="let p of person|filterPerson:searchText: 'name' ; let i = index" >
<td>{{p.name}}</td>
</tr>
</table>
</div>