如何基于angular 7中的多个属性进行过滤

如何基于angular 7中的多个属性进行过滤,angular,filter,properties,pipe,Angular,Filter,Properties,Pipe,我刚接触Angular,现在正在学习如何创建管道来过滤数据。我可以这样做,但是有人能给我一些关于如何基于多个属性进行过滤的想法吗 <mat-card-content> <div *ngFor="let property of item.properties"> {{ (property.labelVisible ? property.label + ' ' : '') + property.value }} </div> 按名称筛选 <

我刚接触Angular,现在正在学习如何创建管道来过滤数据。我可以这样做,但是有人能给我一些关于如何基于多个属性进行过滤的想法吗

<mat-card-content>
  <div *ngFor="let property of item.properties">
    {{ (property.labelVisible ? property.label + ' ' : '') + property.value }}
  </div>
按名称筛选

 <mat-card  *ngFor="let item of model | searchFilter:searchTerm">
<mat-card-title>
  {{ item.title }}
</mat-card-title>

{{item.title}
现在我必须根据项目属性进行筛选

<mat-card-content>
  <div *ngFor="let property of item.properties">
    {{ (property.labelVisible ? property.label + ' ' : '') + property.value }}
  </div>

{{(property.labelVisible?property.label+'''':)+property.value}
搜索过滤管

import {Pipe, PipeTransform} from '@angular/core';
import {Card} from "./card";

@Pipe({
name:'searchFilter'
})
export class SearchFilterPipe implements PipeTransform{
transform(model: Card<any>[], searchTerm:string):Card<any>[]
{
if(!model || !searchTerm){
  return model
}
return model.filter(item =>item.title.toLowerCase().indexOf(searchTerm.toLowerCase())!== -1)
}
}
从'@angular/core'导入{Pipe,PipeTransform};
从“/Card”导入{Card};
@烟斗({
名称:'searchFilter'
})
导出类SearchFilterPipe实现PipeTransform{

transform(型号:Card

我强烈建议不要使用管道过滤数据,这会导致性能损失,而且它还可以防止项目的过度缩小,附录部分对此进行了解释

更好的选择是只调用组件函数进行所需的过滤,或者如果您需要在许多地方使用相同的过滤,您可以在组件文件中为searchTerm使用注入的angular services,不要将其设置为字符串,将其设置为具有所有对象属性的对象,如下图所示 searchTerm={title:,位置:“…} 和在搜索过滤器管道中** 从“@angular/core”导入{Pipe,PipeTransform}; 从“/Card”导入{Card}; @烟斗({ 名称:'searchFilter' }) 导出类SearchFilterPipe实现PipeTransform{ 转换(模型:卡[],搜索词:对象):卡[]{ 如果(!model | |!searchTerm){ 回归模型 } 返回模型.过滤器(项=>{ 返回对象.键(项).过滤器(函数(键,索引){ 返回 item[key].toLowerCase().indexOf(searchTerm[key].toLowerCase()) !== -1 }).长度>0; }) } } *或* 使用模块ngx过滤管,使生活更轻松 do npm i-S ngx过滤管 然后导入模块appModule,然后在组件中添加以下代码 sampleData=[{id:1,名称:'xyz',位置:'india'}, {id:2,名称:'abc',位置:'USA'} ] userFilter:any={name:'',位置:'}; HTML部分如下所示 //在此处添加多个输入的搜索框
    {{user.name} 没有匹配的元素

我认为循环搜索词的动态函数是更好的方法。但是这个解决方案肯定会解决问题。tnx!@valhallarossferer tnx为了你的建议,现在我编辑了答案,在不更改代码的情况下处理n个列。我正在使用ngx过滤管道。我还有一个疑问。如果属性在一个数组中,就像我上面所附的图片一样