1

Hi I'm trying to create a simple search filter that filters an array of employees and returns an array of employees whose names contain the letters typed in the search box. I have imported the filter pipe, and declared it under the provider. I don't get any errors but it also doesn't do anything.. any idea??

I have created the custom filter pipe in filter.pipe.ts:

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

@Pipe({
  name: 'filter',
  pure: false
})
export class FilterPipe implements PipeTransform {

  transform(employees: any, term: any): any {
    //check if search term is undefined
    if (term === undefined) return employees;
    //return updated employees array
    return employees.filter(function(employee){
      return employee.name.toLowerCase().includes(term.toLowerCase());
    })
  }

}

I the "term" variable in the search input in app.component.ts:

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

@Component({
  selector: 'app-root',
  templateUrl: `
  <div class="col-sm-12 col-md-12">
    Search By:
    <div class=navbar-form role="search">
      <div class="input-group">
        <input type="text" [(ngModel)]="term" class="form-control" placeholder="Last name, First name">
      <div class="input-group-btn">
        <button class="btn btn-default" type="submit"><span class="glyphicon glyphicon-search"></span></button>
      </div>
      </div>
      <div class="row" *ngIf="term">
        <h3>You searched for: {{term}}</h3>
      </div>
    </div>
  </div>
  <app-employee></app-employee>
  `,
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app works!';

}

and it is being filtered in my employee-list.component.ts:

import { Component, OnInit } from '@angular/core';

import { IEmployee } from './shared/interface';
import { SharedService } from './shared/shared.service';
import { EMPLOYEES } from './mock-employees';
import { FilterPipe } from './filter.pipe';

// import {Observable, Subject} from '@reactivex/rxjs';

@Component({
  selector: 'app-employee',
  templateUrl: `
  <p>
    Emoloyee List:
  </p>
  <ul *ngFor = 'let employee of employees | filter:term'>
    <li>{{employee.name}}</li>
    <li>{{employee.city}}</li>
  </ul>
  `,
  styles: [],

})
export class EmployeeListComponent implements OnInit {

  employees: IEmployee[] = [
    {"name":"Sarah", "city": "Barcelona"},
    {"name": "Lauren", "city": "Milan"},
    {"name": "Jenny", "city": "Toronto"}
  ];

  constructor( private sharedService: SharedService ) { }

  ngOnInit(): void{

  }

}

and my app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppComponent } from './app.component';
import { EmployeeListComponent } from './employee-list.component';

import { SharedService } from './shared/shared.service';
import { FilterPipe } from './filter.pipe';

@NgModule({
  declarations: [
    AppComponent,
    EmployeeListComponent,
    FilterPipe
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule
  ],
  providers: [ SharedService, FilterPipe ],
  bootstrap: [AppComponent],


})
export class AppModule { }

the list is not being filtered at all. I don't understand why it's not working... Any help would be much appreciated! Thanks!

1 Answer 1

1

Sure it's not working, because term has never been defined inside EmployeeListComponent. Just add an Input to your EmployeeListComponent:

@Input('search-term')
public term: string = null;

And modify your app components HTML to pass in your search term:

 <app-employee [search-term]="term"></app-employee>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much it worked! I had the two way data binding in [(ngModel)]= "term", so I thought that was sufficient enough to be called around components! Thanks so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.