多个过滤器功能无法正常工作
Multiple filters functionality not working properly
我有一个对象数组,格式如下。如果 IsEnabled
为 0
,则 UI 中显示的值为 Disabled
,对于 IsEnabled
值为 1
,则值为 Enabled
.
[
{
AlertID: "A1"
DisplayName: "B1"
Group: "C1"
IsEnabled: 0
},
{
AlertID: "A2"
DisplayName: "B2"
Group: "C2"
IsEnabled: 0
},
]
我有一个函数可以过滤掉所有 4 列的数据。当我尝试从 IsEnabled 中过滤掉值时,其他过滤器不起作用。仅过滤启用和禁用单独工作正常。
multiFilter(data: any[], filters) {
const filterableColumns = Object.keys(this.sortedsearchkeys);
const result = filterableColumns.reduce((acc, field) => {
if (Array.isArray(filters[field]) && filters[field].length) {
if(filters.IsEnabled[0] === 'Enabled') {
return acc.filter((item) => {
return item.IsEnabled == 1
});
}
else if(filters.IsEnabled[0] === 'Disabled') {
return acc.filter((item) => {
return item.IsEnabled == 0
});
}
return acc.filter((item) => {
return filters[field]?.length && filters[field].indexOf(item[field]) > -1
});
}
else {
return acc;
}
}, data);
return result;
}
我不知道如何过滤掉 Enabled/Disabled
以及其他 2 列。
您在 每次 迭代中检查 IsEnabled
,而不管 field
是什么。相反,您应该检查 field
是否真的与 IsEnabled
相关,然后才执行特定于该字段的逻辑:
if (field == "IsEnabled") {
let target = ["Disabled", "Enabled"].indexOf(filters.IsEnabled[0]);
return acc.filter((item) => {
return item.IsEnabled == target;
});
}
我有一个对象数组,格式如下。如果 IsEnabled
为 0
,则 UI 中显示的值为 Disabled
,对于 IsEnabled
值为 1
,则值为 Enabled
.
[
{
AlertID: "A1"
DisplayName: "B1"
Group: "C1"
IsEnabled: 0
},
{
AlertID: "A2"
DisplayName: "B2"
Group: "C2"
IsEnabled: 0
},
]
我有一个函数可以过滤掉所有 4 列的数据。当我尝试从 IsEnabled 中过滤掉值时,其他过滤器不起作用。仅过滤启用和禁用单独工作正常。
multiFilter(data: any[], filters) {
const filterableColumns = Object.keys(this.sortedsearchkeys);
const result = filterableColumns.reduce((acc, field) => {
if (Array.isArray(filters[field]) && filters[field].length) {
if(filters.IsEnabled[0] === 'Enabled') {
return acc.filter((item) => {
return item.IsEnabled == 1
});
}
else if(filters.IsEnabled[0] === 'Disabled') {
return acc.filter((item) => {
return item.IsEnabled == 0
});
}
return acc.filter((item) => {
return filters[field]?.length && filters[field].indexOf(item[field]) > -1
});
}
else {
return acc;
}
}, data);
return result;
}
我不知道如何过滤掉 Enabled/Disabled
以及其他 2 列。
您在 每次 迭代中检查 IsEnabled
,而不管 field
是什么。相反,您应该检查 field
是否真的与 IsEnabled
相关,然后才执行特定于该字段的逻辑:
if (field == "IsEnabled") {
let target = ["Disabled", "Enabled"].indexOf(filters.IsEnabled[0]);
return acc.filter((item) => {
return item.IsEnabled == target;
});
}