Questions
Questions

Reputation: 269

Angular Filter pipe not working properly

I have first name and last name in two different columns in my database, so I had to bind it this way to show it as 1 in my table.

However my search function isn't working when I type in space between first and last name:

<tbody>
    <tr *ngFor="let e of employeelist | filter : searchByName">
        <td>{{e.firstName}} {{e.lastName}}</td>


 import { Injectable, Pipe, PipeTransform } from '@angular/core';

  @Pipe({
    name: 'filter'
   })

   @Injectable()
  export class FilterEmployeesPipe implements PipeTransform {
      transform(value: any, input: string) {
    if (input) {
   input = input.toLowerCase();
   //filter je filter() metod iz array.prototype.filter
   return value.filter(function (employee: any) {

       return ((employee.firstName.toLowerCase().indexOf(input)) && 
     (employee.lastName.toLowerCase().indexOf(input))) > -1;
      })
   }
   return value;
    }
     }

Upvotes: 1

Views: 416

Answers (2)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657068

You need to include the space in the string that you use to match the input

return (employee.firstName.toLowerCase() + ' ' employee.lastName.toLowerCase()).indexOf(input) > -1;

otherwise you're comparing apples with pears.

Upvotes: 0

Soumya B. Athani
Soumya B. Athani

Reputation: 378

Use do like this

<td>{{e.firstName + '&nbsp;' +e.lastName}}</td>

Upvotes: 1

Related Questions