I'm currently facing a challenge with implementing a filter pipe that can handle multiple values across multiple attributes in a table.
While I have successfully managed to filter multiple values on a single attribute, I am struggling to achieve the same for multiple values across multiple attributes.
Below is an excerpt from my existing pipe filter implementation which caters to filtering multiple values within a single attribute:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'vFilter',
pure: false
})
export class VFilterPipe implements PipeTransform {
transform(vList: any[], vfilter?: any): any {
if (!vList || !Object.keys(vfilter).length) {
return vList;
}
return vList.filter(item => {
for (let key in vfilter) {
for(let value in vfilter[key]){
if ((item[key] === undefined || item[key] == vfilter[key][value]))
{
return true;
}
}
return false;
}
return false;
});
}
}
Here is the input array provided:
vList = [{'name':'jack','age':'25'},{'name':'sam','age':'25'},{'name':'smith','age':'25'},{'name':'jack','age':'28'}]
vfilter = {'name':['jack','sam'],'age':['25']}
The expected filtered output should be as follows:
vList = [{'name':'jack','age':'25'},{'name':'sam','age':'25'}]
However, the actual result obtained is:
vList = [{'name':'jack','age':'25'},{'name':'sam','age':'25'},{'name':'jack','age':'28'}]
If you have any suggestions or solutions to this logical issue, your help would be greatly appreciated.