Hi,
In my site I have a search box for cities and right now i have to search for whole word such as "New York", but I want to be able to search with portion of the word such as "New" or "York", how to do that?
here is my code:
html:
<label>Filter City:</label>
<input [(ngModel)]='City'>
<button type="button" (click)="onCityFilter()">Search</button>
<button type="button" (click)="onCityFilterClear()">Clear</button>
in ts:
City ='';
SearchCity = '';
onCityFilter(){
this.SearchCity = this.City;
}
onCityFilterClear(){
this.SearchCity = '';
this.City = '';
}
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'filter'
})
export class FilterPipe implements PipeTransform {
transform(value: any[], filterString: string, propName: string): any[] {
const resultArray = [];
if (value.length === 0 || filterString === '' || propName === ''){
return value;
}
for (const item of value){
if (item[propName].toLocaleLowerCase() === filterString.toLocaleLowerCase()) {
resultArray.push(item);
}
}
return resultArray;
}
}