0

I am trying to change the filter logic in angular datatable

export class FilterPipe implements PipeTransform {
    keys = [];
    transform(items: any, args: string): any {
      console.log('Datatable test');
        if (items != null && items.length > 0) {
          let ans = [];

          if (this.keys.length == 0) {
            this.keys = Object.keys(items[0]);
          }

          for (let i of items) {
            for (let k of this.keys) {

              if (String(i[k]).toLowerCase().indexOf(args.toLowerCase()) >= 0) {

                ans.push(i);

                break;
              }
            }
          }
          return ans;
        }
 }
}

I kept a console.log and recompiled the application. The changes are not reflecting.

Someone please give some light on it ?

SmartestVEGA
  • 8,415
  • 26
  • 86
  • 139

1 Answers1

0

You are lacking an else statement that covers the fact that your items are empty or null/undefined.

Also, you can simplify your full text search like this :

export class FilterPipe implements PipeTransform {
  transform(items: Object[], args: string): any {
    console.log('Datatable test');

    if (!items || !items.length) { return []; }

    return items
      .filter(item => Object.keys(item)
        .some(key => item[key].toLowerCase().includes(args.toLowerCase()))
      );
  }
}
  • its still not reflecting BTW – SmartestVEGA Jul 10 '18 at 08:50
  • @SmartestVEGA ([Here it is, fully working](https://stackblitz.com/edit/angular-vgutvm?file=src%2Fapp%2Fapp.component.html)) –  Jul 10 '18 at 08:55
  • I think this is the issue https://stackoverflow.com/questions/34456430/ngfor-doesnt-update-data-with-pipe-in-angular2 – SmartestVEGA Jul 10 '18 at 09:02
  • Well if you posted your HTML code I could have told you that ... That's why you need to provide a [mcve]. Anyway, I'm glad you resolved your issue, please close or delete your question. –  Jul 10 '18 at 09:03
  • Then (for the third time), provide a [mcve] since you have proof that this code is working. –  Jul 10 '18 at 09:09