[英]Angular2 Filter Array of Objects with pipe using RegExp?
經過數小時的關於stackoverflow和google的搜索,我沒有找到要搜索的內容,但確實找到了一些可以替代解決方案的想法。
對象示例:
items = [
{
title: "This is title",
email: "test@test.com",
status: "confirmed"
},
{
title: "This another one",
email: "someone@something.com",
status: "pending"
{
title: "Just a random string",
email: "me@you.co.uk",
status: "pending"
{
]
分辨率:
import { Pipe, PipeTransform } from '@angular/core'; @Pipe({ name: 'filter', pure: false }) export class FilterPipe implements PipeTransform { transform(value: any, args?: any): any { if(args == '') { return value; } let query = args.toLowerCase(); return value.filter(task => task.title.toLowerCase().indexOf(query) > -1 || task.email.toLowerCase().indexOf(query) > -1 || task.status.toLowerCase().indexOf(query) > -1 ); } }
<div *ngFor="item of (items | filter:'this is') "> {{item | json}} </div>
這會給我:
{title: "This is title", email: "test@test.com",status: "confirmed"}
它按原樣工作,但是我的目的是使其與RegExp一起使用,我已經嘗試過,但是由於某些原因,當我使用var something = new RegExp(// some rule)時出現錯誤。
任何想法都非常感謝。
嘗試這個
import {Pipe} from 'angular2/core';
// Tell Angular2 we're creating a Pipe with TypeScript decorators
@Pipe({
name: 'RegexPipe'
})
export class RegexPipe {
transform(value, args?) {
// ES6 array destructuring
let [pattern] = args;
return value.filter(task => {
reg = new Regex(pattern);
found = reg.test(task.title);
return found;
});
}
}
<div *ngFor="item of (items | RegexPipe:'/this is (.)*/') ">
{{item | json}}
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.