Angular 2 filter/search list

前端 未结 12 1269
眼角桃花
眼角桃花 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:51

    try this html code

    <md-input #myInput placeholder="Item name..." [(ngModel)]="name"></md-input>
    
    <div *ngFor="let item of filteredItems | search: name">
       {{item.name}}
    </div>
    

    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);
        });
    }
    
    }
    
    0 讨论(0)
  • 2020-11-27 14:52

    You have to manually filter result based on change of input each time by keeping listener over input event. While doing manually filtering make sure you should maintain two copy of variable, one would be original collection copy & second would be filteredCollection copy. The advantage for going this way could save your couple of unnecessary filtering on change detection cycle. You may see a more code, but this would be more performance friendly.

    Markup - HTML Template

    <md-input #myInput placeholder="Item name..." [(ngModel)]="name" (input)="filterItem(myInput.value)"></md-input>
    
    <div *ngFor="let item of filteredItems">
       {{item.name}}
    </div>
    

    Code

    assignCopy(){
       this.filteredItems = Object.assign([], this.items);
    }
    filterItem(value){
       if(!value){
           this.assignCopy();
       } // when nothing has typed
       this.filteredItems = Object.assign([], this.items).filter(
          item => item.name.toLowerCase().indexOf(value.toLowerCase()) > -1
       )
    }
    this.assignCopy();//when you fetch collection from server.
    
    0 讨论(0)
  • 2020-11-27 14:52

    HTML

    <input [(ngModel)] = "searchTerm" (ngModelChange) = "search()"/>
    <div *ngFor = "let item of items">{{item.name}}</div>
    

    Component

    search(): void {
        let term = this.searchTerm;
        this.items = this.itemsCopy.filter(function(tag) {
            return tag.name.indexOf(term) >= 0;
        }); 
    }
    

    Note that this.itemsCopy is equal to this.items and should be set before doing the search.

    0 讨论(0)
  • 2020-11-27 14:53

    Slight modification to @Mosche answer, for handling if there exist no filter element.

    TS:

    import { Pipe, PipeTransform } from '@angular/core';
    
    @Pipe({
        name: 'filterFromList'
    })
    export class FilterPipe implements PipeTransform {
        public transform(value, keys: string, term: string) {
    
            if (!term) {
                return value
            }
            let res = (value || []).filter((item) => keys.split(',').some(key => item.hasOwnProperty(key) && new RegExp(term, 'gi').test(item[key])));
            return res.length ? res : [-1];
    
        }
    }
    

    Now, in your HTML you can check via '-1' value, for no results.

    HTML:

    <div *ngFor="let item of list | filterFromList: 'attribute': inputVariableModel">
                <mat-list-item *ngIf="item !== -1">
                    <h4 mat-line class="inline-block">
                     {{item}}
                    </h4>
                </mat-list-item>
                <mat-list-item *ngIf="item === -1">
                    No Matches
                </mat-list-item>
            </div>
    
    0 讨论(0)
  • 2020-11-27 14:54

    data

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

    You can achieve this by creating a simple pipe

    <input type="text" [(ngModel)]="queryString" id="search" placeholder="Search to type">
    

    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

    0 讨论(0)
  • 2020-11-27 15:01

    Currently ng2-search-filter simplify this works.

    By directive

    <tr *ngFor="let item of items | filter:searchText">
      <td>{{item.name}}</td>
    </tr>
    

    Or programmatically

    let itemsFiltered = new Ng2SearchPipe().transform(items, searchText);
    

    Practical example: https://angular-search-filter.stackblitz.io

    0 讨论(0)
提交回复
热议问题