Malik Kashmiri Malik Kashmiri - 2 months ago 29
TypeScript Question

search in Angular 2 with First Name, Last Name and Email

Detail



I am trying to implement search pipe through which user can search with email/ firstname/ lastname but right now it iss perfectly fine with email single search item. I want to search same text enter in input with email first name and last name.

Image



enter image description here

Search Pipe



import {Pipe} from '@angular/core';


@Pipe({
name: "search"
})

export class SearchPipe {

transform(value, term) {
console.log(term + ' ' + value);
if (term == null) {
return null;
}
return value.filter((item) => item.Email.includes(term));
}
}


Response



{
"Id":1,
"ApimId":"1",
"FirstName":"Super",
"LastName":"Admin","
"Email":"ahmed@gmail.com"
}

Answer

You have done the hardest part, just change your pipe to filter based on email or first name or last name

import {Pipe} from '@angular/core';


@Pipe({
    name: "search"
})

export class SearchPipe {

    transform(value, term) {
        console.log(term + ' ' + value);
        if (term == null) {
            return null;
        }
        return value.filter((item) => {
            return item.Email.includes(term) || item.FirstName.includes(term) || item.LastName.includes(term);
        });
    }
}
Comments