如何在 angular 中过滤具有多个属性的数组
how to filter array with multiple properties in angular
我在我的 angular 5 项目中创建了一个管道,它将从 html 和过滤器数组中获取输入,并将 return 过滤数组。但目前我正在从 HTML 文件中传递 属性 名称("firstName")。现在我想要那个,它应该从属性 "firstName" 和 "lastName" 中过滤。请告诉我我应该改变什么才能做同样的事情???
search-filter.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'searchFilter'
})
export class SearchFilter implements PipeTransform {
transform(data: any, search?: string, propertyName?: string): any {
if (search === undefined) {
return data;
} else {
return data.filter(obj => obj[propertyName].toLowerCase().includes(search.toLowerCase()));
}
}
}
user.component.html
<div class="row" *ngFor="let data of datas| searchFilter: search: 'firstName'}">
{{data.lastName}}, {{data.firstName}}
</div>
sample-data.ts
datas: [
{ firstName: 'vipin', company: 'abc', lastName: 'sharma' },
{ firstName: 'kevin', company: 'abc2', lastName: 'xyz' },
{ firstName: 'Leos', company: 'abc3', lastName: 'abc' },
]
您可以做的是连接要搜索的属性:
this.items.map((item) => {
return {...item, searchTerms: item.firstname + item.lastname;
});
<li *ngFor="let item of items | searchFilter:search:'searchTerms'>{{item.name}}</li>
你能这样试试吗
this.data.filter((element: datatype) =>element.firstName && element.lastName)
我得到了答案并且工作正常:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'searchFilter'
})
export class SearchFilter implements PipeTransform {
transform(data: any, search?: string, propertyName?: string, propertyName2?: string): any {
if (search === undefined) {
return data;
} else {
let filteredData = data.filter(obj => obj[propertyName].toLowerCase().includes(search.toLowerCase()));
if (propertyName2) {
filteredData = filteredData.concat(data.filter(obj => obj[propertyName2].toLowerCase().includes(search.toLowerCase())));
}
return filteredData;
}
}
}
我在我的 angular 5 项目中创建了一个管道,它将从 html 和过滤器数组中获取输入,并将 return 过滤数组。但目前我正在从 HTML 文件中传递 属性 名称("firstName")。现在我想要那个,它应该从属性 "firstName" 和 "lastName" 中过滤。请告诉我我应该改变什么才能做同样的事情???
search-filter.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'searchFilter'
})
export class SearchFilter implements PipeTransform {
transform(data: any, search?: string, propertyName?: string): any {
if (search === undefined) {
return data;
} else {
return data.filter(obj => obj[propertyName].toLowerCase().includes(search.toLowerCase()));
}
}
}
user.component.html
<div class="row" *ngFor="let data of datas| searchFilter: search: 'firstName'}">
{{data.lastName}}, {{data.firstName}}
</div>
sample-data.ts
datas: [
{ firstName: 'vipin', company: 'abc', lastName: 'sharma' },
{ firstName: 'kevin', company: 'abc2', lastName: 'xyz' },
{ firstName: 'Leos', company: 'abc3', lastName: 'abc' },
]
您可以做的是连接要搜索的属性:
this.items.map((item) => {
return {...item, searchTerms: item.firstname + item.lastname;
});
<li *ngFor="let item of items | searchFilter:search:'searchTerms'>{{item.name}}</li>
你能这样试试吗
this.data.filter((element: datatype) =>element.firstName && element.lastName)
我得到了答案并且工作正常:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'searchFilter'
})
export class SearchFilter implements PipeTransform {
transform(data: any, search?: string, propertyName?: string, propertyName2?: string): any {
if (search === undefined) {
return data;
} else {
let filteredData = data.filter(obj => obj[propertyName].toLowerCase().includes(search.toLowerCase()));
if (propertyName2) {
filteredData = filteredData.concat(data.filter(obj => obj[propertyName2].toLowerCase().includes(search.toLowerCase())));
}
return filteredData;
}
}
}