【问题标题】:How to improve the performance of search filter in angular for a large list如何提高大型列表的角度搜索过滤器的性能
【发布时间】:2018-05-05 17:28:49
【问题描述】:

在搜索栏中键入时,鼠标事件会在键入时暂停几秒钟,直到加载数据(似乎每个单击事件都会触发数据在搜索中加载)这发生在前几次键入时使用搜索过滤器管道)没有搜索管道它仍然很慢。请帮忙谢谢 请点击链接查看stackblitz示例:

https://ionic-6cbe9t.stackblitz.io

searchFilter(searchTerm){
  if(searchTerm != ''){
    searchTerm = searchTerm.toLowerCase();
  }

  if(!this.listFromArray){
    return Immutable.List();
 }

  if(!searchTerm){
    return this.listFromArray;
  }

  return  this.listFromArray.filter((address) => {
    return address.Company.toLowerCase().includes(searchTerm) ||
        address.Phone.toString().includes(searchTerm) ||
        address.Street.toLowerCase().includes(searchTerm) ||
        address.City.toLowerCase().includes(searchTerm)  ||
        address.Phone.includes(searchTerm);
  });
}

【问题讨论】:

  • 在 Observable 中添加 debounceTime 或添加条件以检查长度是否大于 3 个字符...(注意:如果不知道您正在使用的代码,很难尝试提供帮助)
  • 嗨,谢谢,我正在使用简单的搜索过滤器管道。过滤一组项目。然后我将过滤器添加到 *ngFor="let item of Items | filterItems: searchTerm"。
  • 我把如何使用 debounceTime 进行一般搜索。我希望这对你有帮助。无论如何,如果您在加载项目时添加一个新属性“searchTerms”,也许您可​​以改进

标签: javascript angular performance search ionic-framework


【解决方案1】:

如何进行“debounceTime”搜索

你的组件.html

  <input #search (keyup)="searchTerm.next(search.value)">
  <div *ngIf="results"> 
    <!--slice is optional-->
    <div *ngFor="let item of results |slice:0:10">
         {{item.descripcion}}
    </div>
   </div>

在你的 component.ts 中

  //We defined three variables,
  items:any[];  //our items
  results:any[];  //Our items filtered
  searchTerm = new Subject<string>();  //A subject 

  search(terms: Observable<string>) {
    return terms.pipe(debounceTime(400),
      distinctUntilChanged(),
      switchMap(term => this.searchEntries(term)));
  }

  searchEntries(term) {
    //here we can put others conditions to term
    //like term.lenght>3, e.g.
    if (!term)
      return of(null);  //Notice we return an observable -using Observable.of
    term=term.toUpperCase();
    return of(     //Idem before. If we're ask about an observable
                   //Like httpClient.get, we not need "of" 
       this.items.filter(a=>a.searchProperty.indexOf(term)>=0)
    );
  }

  ngOnInit() {
    //we subscribe to get the items
    this.http.get("assets/items.json").subscribe((res:any[])=>{
         //we can do this.items=res ...
         //this.items=res;
         //...but better make a map and create a property "searchProperty"
         //so, our filtered can be over this property. notice
         //the map is executed only one time
         this.items=res.map(x=>{
           return {
                   ...x,
                   searchProperty:x.descriptcion.toUpperCase()
                         +x.texto.toUpperCase()
                         +x.comment.toUpperCase()
                   }
          });
         //then, we subscribe to "this.search"
        this.search(this.searchTerm)
          .subscribe(results => {
             this.results = results;   
        });
    });
}

注意:代码很大程度上来自https://alligator.io/angular/real-time-search-angular-rxjs/

【讨论】:

  • 非常感谢。有没有办法使用管道做到这一点?这是一个可重复使用的搜索
  • 我不想每次搜索时都调用 api。我希望在组件加载时显示列表并在用户键入时过滤列表
  • 代码不会每次都调用api,只是在一次ngOnInit中调用。我不知道如何“在管道中翻译”,但必须很容易地创建一个以@Input 形式接收数据的自定义组件。代码的“key”在 Observable 中转换为搜索的“变化”,不要在每次 seach 变化时都调用一个函数
  • 谢谢。我将尝试使用组件服务类并将 searchTerm 绑定到搜索自定义输入 。为什么代码调用了令人困惑的 api 订阅中的 search() 方法。我有一个从 api 显示的可用列表视图,我将该数组结果存储在 collection[] = apiResults;此数组遍历 ngFor 数组以显示给用户。数组列表是我要过滤的。如何将搜索词绑定到自定义输入?
猜你喜欢
  • 2020-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 2023-02-08
  • 2022-11-27
相关资源
最近更新 更多