Angular 2 filter/search list

前端 未结 12 1400
眼角桃花
眼角桃花 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条回答
  •  忘掉有多难
    2020-11-27 14:54

    data

    names = ['Prashobh','Abraham','Anil','Sam','Natasha','Marry','Zian','karan']
    

    You can achieve this by creating a simple pipe

    
    

    Pipe

    import { Pipe, PipeTransform } from '@angular/core';
    
    @Pipe({
        name: 'FilterPipe',
    })
    export class FilterPipe implements PipeTransform {
        transform(value: any, input: string) {
            if (input) {
                input = input.toLowerCase();
                return value.filter(function (el: any) {
                    return el.toLowerCase().indexOf(input) > -1;
                })
            }
            return value;
        }
    }
    

    This will filter the result based on the search term

    More Info

提交回复
热议问题