首页 > 解决方案 > 我正在尝试使用随附的代码制作过滤表,可以吗?

问题描述

代码附在此处 我已在此消息中附加的照片中附加了代码,因为代码很长。

请帮我检查一下,我正在尝试使用该代码制作一个过滤表。

标签: angular

解决方案


首先,请不要附图片,只复制问题中的重要部分代码,选择并使用Ctrl + K格式化(基于图像的问题真的很难阅读)

其次,使用带有两个参数的简单函数,例如

(click)="filterYear(0,20)"
(click)="filterYear(21,30)"
...

同性别

(click)="filterGender('male')"
(click)="filterGender('female')"
(click)="filterGender(null)"

您需要将性别以及最小和最大年份存储在三个变量中

我想你有两个变量“data”和“dataFiltered”。所以你的功能过滤器应该像

filterYear(min:number,max:number)
{
    this.min=min;
    this.max=max;
    this.filter(); //call to the function filter
}
filtergender(gender:any)
{
    this.gender=gender
    this.filter();
}

filter()
{
  this.dataFiltered=this.data.filter(x=>x.year>=min && x.prop<=year && 
        (!this.gender || x.gender==this.gender)

}

如果 this.gender 没有值或性别等于 this.gender,则看到最后一个条件为真

注意:我不知道您的数据,所以我想您有一个具有“年份和性别”属性的对象数组


推荐阅读