Created
May 10, 2018 11:20
-
-
Save AliwerErgashev/81c8d13273aaee15eb54513a974c48da to your computer and use it in GitHub Desktop.
Angular Pipes Filtering on Multiple Keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul> | |
<li *ngFor="let item of _items | filter:{ label: filterText, description: filterText } : false">{{ item.value }} - {{ item.label }} - {{ item.description }}</li> | |
</ul> | |
@Pipe({ | |
name: 'filter' | |
}) | |
export class FilterPipe implements PipeTransform { | |
transform(items: any, filter: any, isAnd: bool): any { | |
if (filter && Array.isArray(items)) { | |
let filterKeys = Object.keys(filter); | |
if (isAnd) { | |
return items.filter(item => | |
filterKeys.reduce((memo, keyName) => | |
(memo && new RegExp(filter[keyName], 'gi').test(item[keyName])) || filter[keyName] === "", true)); | |
} else { | |
return items.filter(item => { | |
return filterKeys.some((keyName) => { | |
console.log(keyName); | |
return new RegExp(filter[keyName], 'gi').test(item[keyName]) || filter[keyName] === ""; | |
}); | |
}); | |
} | |
} else { | |
return items; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment