我正在尝试使用各种类型(布尔值,字符串匹配等)的多个过滤器来过滤网格。网格只是一个JSON数组。每当设置或删除过滤器值(即选中或未选中复选框)时,我都必须过滤。我试图使用一个非常复杂的switch语句来执行此操作,但是后来我发现@ ori-drori的this post解释了如何使用更简单的filterHandlers映射来执行此操作。但是,当我运行这段代码时,我没有得到想要的结果,而我为之困惑。这就是我所拥有的:
filterGrid(rows) {
const filterList = [
{ 'open': this.onlyOpen },
{ 'assmtComplete': this.onlyAssmtCompleted },
{ 'bureau': this.selectedBureau ? this.selectedBureau : '' },
{ 'unit': this.selectedUnit ? this.selectedUnit : '' }
];
const filterHandlers = new Map([
[
'open',
(ref) => !ref.cwsClosureReasonDescription
],
[
'assmtComplete',
(ref) => ref.safetyAsstComplDate !== null
],
[
'bureau',
(ref, val) => ref.bureauCode === val
],
[
'unit',
(ref, val) => ref.unitID === val
],
]);
const applyFilters = (arr, filters) => {
const filterKeys = Object.keys(filters);
return arr.filter(o => filterKeys.every((key) => {
const handler = filterHandlers.get(key); // THIS returns undefined every time
return !handler || handler(o[key], filters[key]);
}));
};
const result = applyFilters(rows, filterList);
console.log(result);
}
“行”中的示例对象可能看起来像这样:
{
createdDate: "2019-10-18T10:56:43.477"
closureReasonDescription: null
refNumber: "1231-3213-2132-1321321"
incidentZipCode: "92108"
intakeDate: "2019-10-19T00:56:24.953"
referralId: 1461
bureauCode: "S"
unitID: 1017
safetyAsstComplDate: null
taskTypeId: null
}
从上面的代码的注释中可以看到,applyFilters中的这一行始终是未定义的。我尝试了几种从中获取处理程序的方法,但是我不确定应该使用哪个键-整数还是字符串?任何提示将不胜感激!
答案 0 :(得分:1)
filterKeys
(由Object.keys(filterList)
定义)是数字键(["0", "1", "2", "3"]
)的数组,因为filterList
是其键是数字索引的数组。
我认为您希望filterKeys
是filterList
数组中每个对象的键。将filterList
做成一个具有多个键的单个对象,而不是使每个对象具有一个键的对象数组最容易实现:
const filterList = {
'open': this.onlyOpen,
'assmtComplete': this.onlyAssmtCompleted,
'bureau': this.selectedBureau ? this.selectedBureau : '',
'unit': this.selectedUnit ? this.selectedUnit : ''
];