我有一个管道,用于过滤客户的日期。现在看起来像:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'filter'
})
export class FilterPipe implements PipeTransform {
transform(clients: any[], searchText: string): any[] {
if(!clients) return [];
if(!searchText) return clients;
searchText = searchText.toLowerCase();
return clients.filter( it => {
if (it.firstName.toLowerCase().includes(searchText)) {
return it.firstName.toLowerCase().includes(searchText);
}
if (it.lastName.toLowerCase().includes(searchText)) {
return it.lastName.toLowerCase().includes(searchText);
}
if (it.street.toLowerCase().includes(searchText)) {
return it.street.toLowerCase().includes(searchText);
}
if (it.city.toLowerCase().includes(searchText)) {
return it.city.toLowerCase().includes(searchText);
}
if (it.zipCode.toLowerCase().includes(searchText)) {
return it.zipCode.toLowerCase().includes(searchText);
}
if (it.country.toLowerCase().includes(searchText)) {
return it.country.toLowerCase().includes(searchText);
}
if (it.email.toLowerCase().includes(searchText)) {
return it.email.toLowerCase().includes(searchText);
}
if (it.phone.toLowerCase().includes(searchText)) {
return it.phone.toLowerCase().includes(searchText);
}
});
}
}
但是我明白,条件可以写得更短。请告诉我,我该怎么办?..
答案 0 :(得分:1)
下面是写条件的一种较短方法:
public void onPostExecute(Map<String, String> result) {
String[] fromwhere = { "NAME","PRICE","SIZE" ,"RANGE","SUPPLIER"};
int[] viewswhere = {R.id.Name_txtView , R.id.price_txtView,R.id.size_txtView};
ADAhere = new SimpleAdapter(getActivity(), result, R.layout.list_valence, fromwhere, viewswhere);
list.setAdapter(ADAhere);
}
答案 1 :(得分:1)
您可以使用这种方式
return clients.filter(function (item: any) {
for (let property in item) {
if (item[property] === null) {
continue;
}
if (item[property].toString().toLowerCase().includes(searchText)) {
return true;
}
}
return false;
});