Angular 2 filter/search list

前端 未结 12 1403
眼角桃花
眼角桃花 2020-11-27 14:25

I\'m looking for the angular 2 way to do this.

I simply have a list of items, and I want to make an input whos job is to filter the list.



        
12条回答
  •  猫巷女王i
    2020-11-27 14:51

    try this html code

    
    
    
    {{item.name}}

    use search pipe

    import { Pipe, PipeTransform } from '@angular/core';
    
    @Pipe({
      name: 'search'
    })
    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);
        });
    }
    
    }
    

提交回复
热议问题