【发布时间】:2016-03-21 12:16:28
【问题描述】:
我正在尝试在 Angular2 中创建一个过滤器管道,它必须通过包含多个嵌套对象的数组进行过滤。这些对象来自 Salesforce,有时会包含嵌套对象,如下例所示:
Object {
Id: "a0436000001awB5AAI",
Name: "Some product",
Store__c: "a0436000001awC2JJU",
Product__c: "a0136000001UGzzAAG",
Product__r: Object {
Id: "a0136000001UGzzAAG",
Name: "Parent product",
...
},
...
}
正常的排序方法效果不佳,因为它们往往不会进入多个级别。我一直在尝试自己写,但我似乎无法弄清楚。这是我现在拥有的:
// # Filter Array of Objects
@Pipe({ name: 'filter' })
export class FilterArrayPipe implements PipeTransform {
transform(value, args) {
let filterKeys: string[];
if (args[1]) {
let parts = args[1].replace(' ', '').split(',');
filterKeys = parts;
}
if (!args[0]) {
return value;
} else if (value) {
return value.filter(item => {
for (let key in item) {
if ((typeof item[key] === 'string' || item[key] instanceof String && item[key]) && (item[key].indexOf(args[0]) !== -1)) {
if (filterKeys && filterKeys.length > 0) {
if (item[key] in filterKeys) {
return true;
}
}
else {
return true;
}
}
}
});
}
}
}
它现在根本不起作用。
【问题讨论】:
-
你想用这个管道过滤器实现什么?
-
您的代码建议您尝试像这样过滤:"*ngFor="#i of someArray | filter:'a0136000001UGzzAAG':'Id,Sotre__c' "。我是对的吗?另外,您使用的 'in' 运算符是错误的。在 'if (item[key] in filterKeys)' 行中,您正在使用它数组,而它应该与对象一起使用。
-
@Abdulrahman 你是完全正确的关于我如何尝试使用管道。
-
@A_Singh 我将过滤器附加到名为 queueD ilter 的 ngModel 并尝试过滤表中的记录
标签: javascript filter angular