2018-07-06 18:40:08 +02:00
|
|
|
import { Pipe, PipeTransform } from "@angular/core";
|
|
|
|
|
|
|
|
@Pipe({
|
|
|
|
name: "search",
|
|
|
|
})
|
|
|
|
export class SearchPipe implements PipeTransform {
|
2018-07-06 21:03:00 +02:00
|
|
|
transform(
|
|
|
|
items: any[],
|
|
|
|
searchText: string,
|
|
|
|
prop1?: string,
|
|
|
|
prop2?: string,
|
|
|
|
prop3?: string
|
|
|
|
): any[] {
|
2018-07-06 18:40:08 +02:00
|
|
|
if (items == null || items.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (searchText == null || searchText.length < 2) {
|
2018-07-06 21:03:00 +02:00
|
|
|
return items;
|
2018-07-06 18:40:08 +02:00
|
|
|
}
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2018-07-06 20:11:47 +02:00
|
|
|
searchText = searchText.trim().toLowerCase();
|
2021-02-04 16:49:23 +01:00
|
|
|
return items.filter((i) => {
|
2021-12-16 13:36:21 +01:00
|
|
|
if (
|
2018-07-06 18:40:08 +02:00
|
|
|
prop1 != null &&
|
|
|
|
i[prop1] != null &&
|
2018-07-06 21:03:00 +02:00
|
|
|
i[prop1].toString().toLowerCase().indexOf(searchText) > -1
|
2021-12-16 13:36:21 +01:00
|
|
|
) {
|
2018-07-06 18:40:08 +02:00
|
|
|
return true;
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
if (
|
2018-07-06 18:40:08 +02:00
|
|
|
prop2 != null &&
|
|
|
|
i[prop2] != null &&
|
|
|
|
i[prop2].toString().toLowerCase().indexOf(searchText) > -1
|
2021-12-16 13:36:21 +01:00
|
|
|
) {
|
2018-07-06 18:40:08 +02:00
|
|
|
return true;
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
|
|
|
if (
|
2018-07-06 21:03:00 +02:00
|
|
|
prop3 != null &&
|
|
|
|
i[prop3] != null &&
|
|
|
|
i[prop3].toString().toLowerCase().indexOf(searchText) > -1
|
2021-12-16 13:36:21 +01:00
|
|
|
) {
|
2018-07-06 18:40:08 +02:00
|
|
|
return true;
|
2021-12-16 13:36:21 +01:00
|
|
|
}
|
2018-07-06 18:40:08 +02:00
|
|
|
return false;
|
2021-12-16 13:36:21 +01:00
|
|
|
});
|
|
|
|
}
|
2018-07-06 18:40:08 +02:00
|
|
|
}
|