代码之家  ›  专栏  ›  技术社区  ›  naveen

如何使用角度2中的管道过滤列表

  •  1
  • naveen  · 技术社区  · 6 年前

    你能告诉我如何用角2的管道过滤列表吗

    https://stackblitz.com/edit/angular-qvtqeu?file=src%2Fapp%2Fapp.component.html

    我这样试过

    <ul class="user-list | filterlist:userenter">
      <li *ngFor="let user of users" class="user-list__item">
    

    滤波器

    import { Pipe, PipeTransform } from '@angular/core';
    
    @Pipe({
      name: 'filterlist'
    })
    export class FilterlistPipe implements PipeTransform {
    
      transform(value: any, args?: any): any {
        return value.filter(
          item => item.first_name.toLowerCase().indexOf(args.toLowerCase()) > -1
       );
      }
    
    }
    

    但是当我在输入框上输入时过滤不起作用?

    5 回复  |  直到 6 年前
        1
  •  3
  •   Pranay Rana    6 年前

    Working Demo

    你应该这样做

    import { Pipe, PipeTransform } from '@angular/core';
    
    @Pipe({
      name: 'filterlist'
    })
    export class FilterlistPipe implements PipeTransform {
    
      transform(value: any, args?: any): any {
        if(!args)
         return value;
        return value.filter(
          item => item.first_name.toLowerCase().indexOf(args.toLowerCase()) > -1
       );
      }
    }
    

    检查 args 是否有价值,第一次你将没有价值 阿尔茨海默病 …这就是它不起作用的原因

        2
  •  2
  •   Cobus Kruger    5 年前

    Angular之所以没有配备管道,是因为它的性能很差。

    对于数组中的每一行,您将迭代整个数组。可能每秒重复几次。这不是你想要的。

    相反,请这样声明您的列表:

    allUsers: [];
    filteredUsers: [];
    

    填充 allUsers 就像你现在做的那样 users . 然后,在每个地方 searchText 更改,迭代 所有用户 并将匹配的用户添加到 filteredUsers . 这样,如果只有五个用户与您的搜索文本匹配,模板只需要迭代五次。

    你的循环变成:

    <ul class="user-list">
      <li *ngFor="let user of filteredUsers" class="user-list__item">
    </ul>
    

    等等。

    我应该补充一下,自从我第一次发布这个答案以来,每当我想减少模板中的工作量时,我也使用了相同的技术。我发现,只有你的模板迭代一千次才能在旧的移动设备上表现非常差,甚至在我的i7开发PC上也会引起明显的延迟。

        3
  •  0
  •   Bhanu Tej P    6 年前

    如果args为空,则返回所有项。

    import { Pipe, PipeTransform } from '@angular/core';
    
     @Pipe({
      name: 'filterlist'
     })
     export class FilterlistPipe implements PipeTransform {
    
     transform(value: any, args?: any): any {
      // added code
      if(args == null){
        return value;
       }
     // added code
    
       return value.filter(
         item => item.first_name.toLowerCase().indexOf(args.toLowerCase()) > -1
      );
     }
    
    }
    
        4
  •  0
  •   Jordan.J.D    6 年前

    你可以使用NG2搜索过滤器NPM。
    更多细节,您可以通过这个演示: Demo Link

    应用模块.ts

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

    应用组件

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: [ './app.component.css' ]
    })
    export class AppComponent  {
      title = 'Angular Search Using ng2-search-filter';
      searchText;
      heroes = [
        { id: 11, name: 'Mr. Nice', country: 'India' },
        { id: 12, name: 'Narco' , country: 'USA'},
        { id: 13, name: 'Bombasto' , country: 'UK'},
        { id: 14, name: 'Celeritas' , country: 'Canada'},
        { id: 15, name: 'Magneta' , country: 'Russia'},
        { id: 16, name: 'RubberMan' , country: 'China'},
        { id: 17, name: 'Dynama' , country: 'Germany'},
        { id: 18, name: 'Dr IQ' , country: 'Hong Kong'},
        { id: 19, name: 'Magma' , country: 'South Africa'},
        { id: 20, name: 'Tornado' , country: 'Sri Lanka'}
      ];
    }
    

    app.component.html软件

    <div class="container text-center">
      <h1>{{title}}</h1>
    </div>
    <div class="container">
      <div class="row">
        <div class="search-hero">
          <input class="form-control" type="text" name="search" [(ngModel)]="searchText" autocomplete="off" placeholder="&#61442;  Start searching for a hero by id or name or country">
        </div>
        <table class="table table-striped">
          <thead>
          <tr>
            <th>Id</th>
            <th>Hero Name</th>
            <th>Country</th>
          </tr>
          </thead>
          <tbody>
          <tr *ngFor="let hero of heroes | filter:searchText">
            <td>{{hero.id}}</td>
            <td>{{hero.name}}</td>
            <td>{{hero.country}}</td>
          </tr>
          </tbody>
        </table>
      </div>
    </div>
    

    这将负责从结果列表中筛选数据。 希望这对你有帮助。

        5
  •  0
  •   Jordan.J.D    6 年前

    您已将筛选器应用于 class 属性…

    应该在里面 *ngFor :

    *ngFor="let user of users | filterlist:userenter"
    

    在管道代码(filterlist.pipe.ts)中,如果没有过滤器集,则返回整个数组:

    if(args === undefined){
      return value;
    }