【问题标题】:How can I filter (with multiple combinations) the data of a table?如何过滤(使用多种组合)表的数据?
【发布时间】:2021-10-14 18:46:09
【问题描述】:

我的 Angular 项目有问题。 我有 4 个选择框,您可以在其中选择不同的过滤器:

  • 上班族
  • 项目名称
  • 员工
  • 活动

我的问题是我很难预测所有可能的过滤器组合 例子: 员工项目名称、员工类型、员工活动......等等。

我尝试过使用过滤器,但它似乎没有给我正确的数据。

  entities: Activity[],
  { query, employee, type, project, date_from, date_to }: { query: string, employee: string[] , type : string[], project: string[],date_from: Date, date_to: Date },
): Activity[] {
  
    return employee || type|| project?
    entities.filter(e => 
      employee?.indexOf(`${e.employee.firstName.toLowerCase()} ${e.employee.lastName.toLowerCase()}`)>-1 
    
  
      ).filter(e => {type?.indexOf(`${e.type.name.toLowerCase()}`)>-1})
      :entities


}

这是我尝试采用的方法,因此如果数组(例如使用的)为空,则执行下一次过滤(对类型)。

【问题讨论】:

  • 你真的在使用 Angular 1.x 吗?这就是你标记这个q的方式......
  • 对不起,我已经更新了。我使用最后一个角度版本。

标签: angular typescript filter


【解决方案1】:

一般你可以创建一个filterObject

filter={officeWorker:..,projectName:..,employed:...,activities:..}

属性可以为null或者有值,那么你就简单的使用(!filter.property || x.property.indexOf(filter.property)>-1) &&的方式

  this activitiesFiltered=this.activities.filter(x=>{
      (!filter.officeWorker || x.officeWorker.toLowerCase().indexOf(filter.officeWorker)>-1) &&
      (!filter.projectName|| x.projectName.toLowerCase().indexOf(filter.projectName)>-1) &&
       ....
  })

我发了fool stackblitz

【讨论】:

  • mmm 不,这样只有当我完成所有选择框时才会搜索。
  • 我加了一个傻瓜stackblitz
猜你喜欢
  • 1970-01-01
  • 2011-04-27
  • 1970-01-01
  • 1970-01-01
  • 2015-09-20
  • 2020-10-31
  • 2019-09-24
  • 2011-01-18
  • 2019-12-28
相关资源
最近更新 更多