I created a custom pipe that filters the search field and displays a list of items based on the search text. Currently, it only filters by companyDisplay, but I want to also filter by companyCode and companyName.
JSON
[{
companyDisplay: "ABC",
companyName: "EFG",
companyCode: "1234"
}]
Search Pipe
import { Pipe, PipeTransform } from '@angular/core'; // search text sorting
@Pipe({
name: 'searchCompanyPipe'
})
export class SearchPipe implements PipeTransform {
transform(items: any[], searchText: any): any[] {
if (!items) { return []; }
if (!searchText) { return items; }
searchText = searchText.toLowerCase();
return items.filter(i => {
return (i.companyDisplay || i.companyCode).toLowerCase().includes(searchText);
});
}
}
HTML
<li *ngFor="let data of companyInfo | searchPipe:searchValue">
The companyInfo
variable contains the JSON data.