After hours of searching on stackoverflow and google I did not find what I was searching, I did find something that gave me an idea for an alternative solution.
Object example:
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"
{
]
The resolution:
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>
This will give me:
{title: "This is title", email: "test@test.com",status: "confirmed"}
It works as is, but my intention was to make it work with RegExp, I have tried but for some reason I got an error when I used var something = new RegExp(// some rule).
Any idea is much appreciated.
try this
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.