【发布时间】:2020-02-04 04:06:24
【问题描述】:
我构建了一个自定义组件来过滤一组对象。过滤器使用按钮,从活动设置为非活动,并允许同时打开/关闭多个选项。
我尝试的 StackBlitz - https://stackblitz.com/edit/timeline-angular-7-ut6fxu
在我的演示中,您将看到北、南和东的 3 个按钮/选项。通过单击一个,您可以将其激活,结果应该包括或排除匹配的“位置”,无论是北、南还是东。
我已经创建了我的方法和结构来进行过滤,我正在努力解决最后的逻辑。
到目前为止,我已经创建了一个方法来创建过滤位置数组,具体取决于用户从 3 个按钮中单击的内容。
接下来,这将传递给我的“过滤器数组”,它获取应该将此过滤后的数组与原始数组进行比较的逻辑,以带回仍然剩余的结果数组。
它不太好用,也不知道为什么 - 我最初是通过使用管道来实现这个功能的,但出于某种原因不想朝那个方向发展。
//the action
toggle(location) {
let indexLocation = this.filteredLocations.indexOf(location);
if (indexLocation >= 0) {
this.filteredLocations = this.filteredLocations.filter(
i => i !== location
);
} else {
this.filteredLocations.push({ location });
}
this.filterTimeLine();
}
// the filter
filterTimeLine() {
this.filteredTimeline = this.timeLine.filter(x =>
this.contactMethodFilter(x)
);
}
//the logic
private contactMethodFilter(entry) {
const myArrayFiltered = this.timeLine.filter(el => {
return this.filteredLocations.some(f => {
return f.location === el.location;
});
});
}
【问题讨论】:
标签: javascript arrays angular typescript filter