我想通过单个mat输入搜索来过滤特定于列的值。我在stackbilz中遍历了不同的列,其中有针对不同列的不同搜索输入。任何可以使用过滤谓词??
通过单个mat输入搜索过滤所有列的示例答案 0 :(得分:0)
使用grd-filter.pipe.ts
进行尝试
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'grdFilter'
})
export class GrdFilterPipe implements PipeTransform {
transform(items: any, filter: any, defaultFilter: boolean): any {
if (!filter){
return items;
}
if (!Array.isArray(items)){
return items;
}
if (filter && Array.isArray(items)) {
let filterKeys = Object.keys(filter);
if (defaultFilter) {
return items.filter(item =>
filterKeys.reduce((x, keyName) =>
(x && new RegExp(filter[keyName], 'gi').test(item[keyName])) || filter[keyName] == "", true));
}
else {
return items.filter(item => {
return filterKeys.some((keyName) => {
return new RegExp(filter[keyName], 'gi').test(item[keyName]) || filter[keyName] == "";
});
});
}
}
}
}
.ts文件
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-customer',
templateUrl: './customer.component.html',
})
export class CustomerComponent implements OnInit {
public searchText : string;
public customerData : any;
constructor() { }
ngOnInit() {
this.customerData = [
{"name": 'Anil kumar', "Age": 34, "blog" :'https://code-view.com'},
{"name": 'Sunil Kumar Singh', "Age": 28, "blog" :'https://code-sample.xyz'},
{"name": 'Sushil Singh', "Age": 24, "blog" :'https://code-sample.com'},
{"name": 'Aradhya Singh', "Age": 5, "blog" :'https://code-sample.com'},
{"name": 'Reena Singh', "Age": 28, "blog" :'https://code-sample.com'},
{"name": 'Alok Singh', "Age": 35, "blog" :'https://code-sample.com'},
{"name": 'Dilip Singh', "Age": 34, "blog" :'https://code-sample.com'}];
}
}
.html
<input [(ngModel)]="searchText" placeholder="Search.." class="advancedSearchTextbox">
<p></p>
<table *ngFor="let emp of customerData | grdFilter: {name: searchText, Age:searchText, blog: searchText}; let i=index;">
<tr>
<td style="width: 5%;">{{i +1}}</td>
<td style="width: 10%;">{{emp.name}}</td>
<td style="width: 5%;">{{emp.Age}}</td>
<td style="width: 15%;">{{emp.blog}}</td>
</tr>
</table>