Datatable filter logic change not reflecting

2019-08-26 19:57发布

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 ?

1条回答
Bombasti
2楼-- · 2019-08-26 20:32

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()))
      );
  }
}
查看更多
登录 后发表回答