Angular 4 Pipe Filter

后端 未结 4 1094
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-28 08:30

I am trying to use a custom pipe to filter my *ngFor loop using an input field with ngModel. With my other custom pipe (sortBy), it works perfectly fine. Howeve

4条回答
  •  失恋的感觉
    2020-11-28 09:13

    Pipes in Angular 2+ are a great way to transform and format data right from your templates.

    Pipes allow us to change data inside of a template; i.e. filtering, ordering, formatting dates, numbers, currencies, etc. A quick example is you can transfer a string to lowercase by applying a simple filter in the template code.

    List of Built-in Pipes from API List Examples

    {{ user.name | uppercase }}
    

    Example of Angular version 4.4.7. ng version


    Custom Pipes which accepts multiple arguments.

    HTML « *ngFor="let student of students | jsonFilterBy:[searchText, 'name'] "
    TS   « transform(json: any[], args: any[]) : any[] { ... }
    

    Filtering the content using a Pipe « json-filter-by.pipe.ts

    import { Pipe, PipeTransform, Injectable } from '@angular/core';
    
    @Pipe({ name: 'jsonFilterBy' })
    @Injectable()
    export class JsonFilterByPipe implements PipeTransform {
    
      transform(json: any[], args: any[]) : any[] {
        var searchText = args[0];
        var jsonKey = args[1];
    
        // json = undefined, args = (2) [undefined, "name"]
        if(searchText == null || searchText == 'undefined') return json;
        if(jsonKey    == null || jsonKey    == 'undefined') return json;
    
        // Copy all objects of original array into new Array.
        var returnObjects = json;
        json.forEach( function ( filterObjectEntery ) {
    
          if( filterObjectEntery.hasOwnProperty( jsonKey ) ) {
            console.log('Search key is available in JSON object.');
    
            if ( typeof filterObjectEntery[jsonKey] != "undefined" && 
            filterObjectEntery[jsonKey].toLowerCase().indexOf(searchText.toLowerCase()) > -1 ) {
                // object value contains the user provided text.
            } else {
                // object didn't match a filter value so remove it from array via filter
                returnObjects = returnObjects.filter(obj => obj !== filterObjectEntery);
            }
          } else {
            console.log('Search key is not available in JSON object.');
          }
    
        })
        return returnObjects;
      }
    }
    

    Add to @NgModule « Add JsonFilterByPipe to your declarations list in your module; if you forget to do this you'll get an error no provider for jsonFilterBy. If you add to module then it is available to all the component's of that module.

    @NgModule({
      imports: [
        CommonModule,
        RouterModule,
        FormsModule, ReactiveFormsModule,
      ],
      providers: [ StudentDetailsService ],
      declarations: [
        UsersComponent, UserComponent,
    
        JsonFilterByPipe,
      ],
      exports : [UsersComponent, UserComponent]
    })
    export class UsersModule {
        // ...
    }
    

    File Name: users.component.ts and StudentDetailsService is created from this link.

    import { MyStudents } from './../../services/student/my-students';
    import { Component, OnInit, OnDestroy } from '@angular/core';
    import { StudentDetailsService } from '../../services/student/student-details.service';
    
    @Component({
      selector: 'app-users',
      templateUrl: './users.component.html',
      styleUrls: [ './users.component.css' ],
    
      providers:[StudentDetailsService]
    })
    export class UsersComponent implements OnInit, OnDestroy  {
    
      students: MyStudents[];
      selectedStudent: MyStudents;
    
      constructor(private studentService: StudentDetailsService) { }
    
      ngOnInit(): void {
        this.loadAllUsers();
      }
      ngOnDestroy(): void {
        // ONDestroy to prevent memory leaks
      }
    
      loadAllUsers(): void {
        this.studentService.getStudentsList().then(students => this.students = students);
      }
    
      onSelect(student: MyStudents): void {
        this.selectedStudent = student;
      }
    
    }
    

    File Name: users.component.html


    Filter by Name:

    Present are Students

提交回复
热议问题