Skip to content

Instantly share code, notes, and snippets.

@palanisamym14
Last active March 11, 2019 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palanisamym14/9ac83cb28b5d0c6026b8790ce8116f8a to your computer and use it in GitHub Desktop.
Save palanisamym14/9ac83cb28b5d0c6026b8790ce8116f8a to your computer and use it in GitHub Desktop.
public searchOption = {'searchFeild': 'tag', 'orderByKey': 'isActive', 'orderBy': 'asc'};
import { Pipe, PipeTransform } from '@angular/core';
export interface SearchOption {
searchFeild: string; // Search By specific object keyName
orderByKey: string; // Order By specific object key
orderBy: string; // OrderBy asc/desc
}
@Pipe({
name: 'searchfilter'
})
export class SearchFilterPipe implements PipeTransform {
transform(items: any[], searchKey: string, options: SearchOption): any[] {
if (!items) { return []; }
if (options.orderByKey !== undefined) {
if (options.orderBy !== 'desc') {
items.sort(this.sortItem(options.orderByKey)).reverse();
} else {
items.sort(this.sortItem(options.orderByKey));
}
}
if (!searchKey) { return items; }
if (searchKey === '' || searchKey === null) { return []; }
return items.filter(e => e[options.searchFeild].toLowerCase().indexOf(searchKey) > -1);
}
sortItem(orderBy) {
return (obja, objb) => {
return obja[orderBy] - objb[orderBy];
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment