Home:ALL Converter>Filter search pipe implementation

Filter search pipe implementation

Ask Time:2017-05-23T21:07:30         Author:Sanjaybxl

Json Formatter

I'm filtering the items using the pipe to filter

My Input filed is in the search.html file and Itemlist in the List.html file

Change in the model is not triggering the pipe transform. Please help. Below is the code snippet.

Search.html

<input placeholder="keyword..." [(ngModel)]="search"/>

List.html

<div *ngFor="let item of items | searchPipe:'name':search ">
  {{item.name}}
</div>

Search.pipe.ts

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

@Pipe({
  name : 'searchPipe',
})
export class SearchPipe implements PipeTransform {
  public transform(value, key: string, term: string) {
    return value.filter((item) => {
      if (item.hasOwnProperty(key)) {
        if (term) {
          let regExp = new RegExp('\\b' + term, 'gi');
          return regExp.test(item[key]);
        } else {
          return true;
        }
      } else {
        return false;
      }
    });
  }
}

Author:Sanjaybxl,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/44135760/filter-search-pipe-implementation
yy