Reputation: 75
I want to implement search filter on data list which is stored in array and I want to apply search filter same like search in data table. as shown in below image. but in that fields(name,address..etc) are not fixed.
How can I achive this?
Upvotes: 3
Views: 8637
Reputation: 37393
if you want to filter by other fields just add them in the pipe:
import { Pipe,PipeTransform} from '@angular/core';
@Pipe({
name: 'searchFilter'
})
export class SearchFilter implements PipeTransform {
transform(items: any[], criteria: any): any {
return items.filter(item =>{
for (let key in item ) {
if((""+item[key]).toLocaleLowerCase().includes(criteria.toLocaleLowerCase())){
return true;
}
}
return false;
});
}
}
@Component({
selector: 'sites-component',
template: `
<input #search (keyup)="0">
<ul>
<li *ngFor="let site of (sites | searchFilter: search.value )">(...)</li>
</ul>
`
})
export class SitesComponent{
sites : Array;
}
dont't forget to declare the pipe in your module.
Upvotes: 7