[英]Filter pipe after more fields in Angular 6
I have this snippet that filters a list after more field. 我有此代码段 ,可在更多字段后过滤列表。
If I check john
and mike
it will result: 如果我检查
john
和mike
,它将导致:
0 - john, g1
0-约翰,g1
1 - mike, g2
1-麦克(M2)
But if I check john
, mike
and g3
(which does not belong to any of these 2 users), because of pipe, it will search for g3
but there is no result: 但是,如果我检查
john
, mike
和g3
(不属于这两个用户中的任何一个),由于管道问题,它将搜索g3
但没有结果:
How can I modify the code, if I check g3
not to result null
, but remain the current filtered list? 如果我检查
g3
不导致结果为null
,而是保留当前的过滤列表,该如何修改代码?
Thank you for your time! 感谢您的时间!
app.ts 应用程序
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
name = 'Angular';
users = [
{ 'id': '0', 'name': 'john', 'group': 'g1' },
{ 'id': '1', 'name': 'mike', 'group': 'g2' },
{ 'id': '2', 'name': 'anne', 'group': 'g3' },
{ 'id': '3', 'name': 'dan', 'group': 'g1' },
{ 'id': '4', 'name': 'zoe', 'group': 'g2' },
]
groupValue: string[] = []
userValue: string[] = []
changeGroup(event) {
const group = event.target.value;
const index = this.groupValue.indexOf(group);
if (index < 0) {
this.groupValue.push(group);
} else {
this.groupValue.splice(index, 1);
}
const newGroupValue = [];
newGroupValue.push.apply(newGroupValue, this.groupValue);
this.groupValue = newGroupValue;
}
changeUser(event) {
const user = event.target.value;
const index = this.userValue.indexOf(user);
if (index < 0) {
this.userValue.push(user);
} else {
this.userValue.splice(index, 1);
}
const newUserValue = [];
newUserValue.push.apply(newUserValue, this.userValue);
this.userValue = newUserValue;
}
}
app.html app.html
<ng-container *ngFor="let user of users; let i=index">
<label class="btn btn-filter" id="bttns">
<input type="checkbox" name="customersUserFilter" autoComplete="off" [value]="user.name" (change)="changeUser($event)">
{{ user.name }}
</label>
</ng-container>
<br>
<ng-container *ngFor="let user of users; let i=index">
<label class="btn btn-filter" id="bttns">
<input type="checkbox" name="customersGroupFilter" autoComplete="off" [value]="user.group" (change)="changeGroup($event)">
{{ user.group }}
</label>
</ng-container>
<pre>You select groups {{ userValue | json }} {{ groupValue | json }}</pre>
<div *ngFor="let user of users | filter2 : 'name' : userValue | filter2 : 'group' : groupValue">
{{ user.id }} - {{ user.name }}, {{ user.group }}
</div>
filter.pipe.ts filter.pipe.ts
import { Pipe, PipeTransform, Injectable } from '@angular/core';
@Pipe({
name: 'filter2'
})
@Injectable()
export class FilterPipe implements PipeTransform {
transform(items: any[], field: string, value: string[]): any[] {
if (!items) {
return [];
}
if (!field || !value || value.length <= 0) {
return items;
}
return items.filter(singleItem => {
return (singleItem != null && singleItem[field] != null && singleItem[field] != undefined && value.indexOf(singleItem[field]) >= 0);
});
}
}
I forked your StackBlitz, let me know if it helped you: https://stackblitz.com/edit/angular-upjdc3 我分叉了您的StackBlitz,让我知道它是否对您有帮助: https ://stackblitz.com/edit/angular-upjdc3
My modifications in the pipe: 我在管道中的修改:
export class FilterPipe implements PipeTransform {
transform(items: any[], filters:{[key: string]: string}): any[] {
return items.reduce((accumulator, currentValue) => {
for(let field in filters) {
if(filters[field].includes(currentValue[field]) && !accumulator.includes(currentValue)) {
return accumulator.concat([currentValue]);
}
}
return accumulator;
}, []);
}
}
And in the template : 并在模板中:
<div *ngFor="let user of users | filter2 : {'name' : userValue, 'group' : groupValue }">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.