问题
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:
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()))
);
}
}
来源:https://stackoverflow.com/questions/51260154/datatable-filter-logic-change-not-reflecting