Angular 4 Filter Search Custom Pipe

后端 未结 6 1871
清歌不尽
清歌不尽 2020-11-28 15:14

So I am trying to build a custom pipe to do a search filter of multiple values in a ngFor loop. I have looked for a number of hours for a good working example, and most of t

6条回答
  •  执念已碎
    2020-11-28 15:29

    I have to implement search functionality in my local and Here is Updated your code. please do this way.

    Here is the code that I have to update.

    directory Structure

    app/
       _pipe/
            search/
              search.pipe.ts
              search.pipe.spec.ts
    app/ 
       app.component.css
       app.component.html
       app.component.ts
       app.module.ts
       app.component.spec.ts
    

    command run for creating pipe

    ng g pipe search
    

    component.html

    
        

    {{lock.User}}

    {{lock.AuthID}}

    {{lock.FormName}}

    {{lock.WinHandle}}

    component.js

    Note: In this file, i have to use dummy records for implementation and testing purpose.

    import { Component, OnInit } from '@angular/core';
    import { FormsModule }   from '@angular/forms';
    
    @Component({
        selector: 'app-root',
        templateUrl: './app.component.html',
        styleUrls: ['./app.component.css']
    })
        export class AppComponent implements OnInit{
        public search:any = '';
        locked: any[] = [];
    
        constructor(){}
    
        ngOnInit(){
            this.locked = [
                {ID: 1, User: 'Agustin', AuthID: '68114', FormName: 'Fellman', WinHandle: 'Oak Way'},
                {ID: 2, User: 'Alden', AuthID: '98101', FormName: 'Raccoon Run', WinHandle: 'Newsome'},
                {ID: 3, User: 'Ramon', AuthID: '28586', FormName: 'Yorkshire Circle', WinHandle: 'Dennis'},
                {ID: 4, User: 'Elbert', AuthID: '91775', FormName: 'Lee', WinHandle: 'Middleville Road'},
            ]
        }
    }
    

    module.ts

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { FormsModule }   from '@angular/forms';
    import { AppComponent } from './app.component';
    import { SearchPipe } from './_pipe/search/search.pipe';
    
    
    @NgModule({
      declarations: [
        AppComponent,
        SearchPipe
      ],
      imports: [
        BrowserModule,
        FormsModule
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    export class AppModule { }
    

    pipe.ts

    import { Pipe, PipeTransform } from '@angular/core';
    
    @Pipe({
        name: 'LockFilter'
    })
    
    export class SearchPipe implements PipeTransform {
        transform(value: any, args?: any): any {
    
            if(!value)return null;
            if(!args)return value;
    
            args = args.toLowerCase();
    
            return value.filter(function(item){
                return JSON.stringify(item).toLowerCase().includes(args);
            });
        }
    }
    

    I hope you are getting the pipe functionality and this will help you.

提交回复
热议问题