2 * Created by cp2122 on 1/4/2018.
4 import { Pipe, PipeTransform } from '@angular/core';
5 import * as _ from 'lodash';
10 export class DataFilterPipe implements PipeTransform {
12 transform(items: any, searchStr: string, keys?: string[][], types ?: string[]): any {
13 if (items != null && items.length > 0 && !_.isNil(searchStr)) {
16 if (_.isNil(keys) || keys.length === 0) {
17 keys = Object.keys(items[0]).map((key)=> new Array(key) );
20 for (const item of items) {
22 for(const key of keys) {
23 if(types && types[index] === 'LIST'){
24 let listVal: string[] = DataFilterPipe.getDeepObjectValueByKeysInList(item, key);
25 if (!_.isNil(listVal) && listVal.filter((val) => val.toLowerCase().includes(searchStr.toLowerCase())).length > 0) {
30 let val: string = DataFilterPipe.getDeepObjectValueByKeys(item, key);
31 if (!_.isNil(val) && val.toLowerCase().includes(searchStr.toLowerCase())) {
43 /**********************************************************************
44 get value from obj data by array of keys.
45 @keys: all table column and keys
47 ************************************************************************/
48 static getDeepObjectValueByKeys(rowData: any , keys: string[]) : string {
49 let obj = rowData[keys[0]];
53 for(let i = 1; i < keys.length ; i++){
56 return _.isNil(obj) ? null : obj.toString();
60 /**********************************************************************
61 get values from obj data by array of keys.
62 @keys: all table column and keys
64 ************************************************************************/
65 static getDeepObjectValueByKeysInList(rowData: any , keys: string[]) : string[] {
66 let obj = rowData[keys[0]];
70 for(let i = 1; i < keys.length-1 ; i++){
73 return _.isNil(obj) ? [] : _.map(obj, keys[keys.length -1 ]);