简体   繁体   English

如何在Angular 4中重置过滤器

[英]How to reset filter in Angular 4

Currently I have multiple filters. 目前,我有多个过滤器。 I would like to reset the filter however, its not working. 我想重置过滤器,但是它不起作用。

showOnlyMyRequest(){
    this.requests = this.requests.filter(request => request.requestedBy === 'John Doe');
  }

showAllRequest(){
    this.requests = this.requests.filter(request => request.requestedBy === '*');
  }

In the above example the showAllRequest() doesn't reset the previous filter. 在上面的示例中, showAllRequest()不会重置先前的过滤器。

From MDN web doc : MDN网络文档

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

So your requests array becomes the one .filter() returned, you should stock your filtered values somewhere else if you need to either show your requests filtered or not. 因此,您的请求数组成为返回的.filter(),如果需要显示是否过滤请求,则应将过滤后的值存储在其他位置。

In case if you decide to send a parameter to your search function from your search text field you can try this. 如果您决定从搜索文本字段向搜索功能发送参数,则可以尝试此操作。 let me know if there is a better solution so I can improve myself too. 让我知道是否有更好的解决方案,以便我也能改善自己。 Thanks in advance. 提前致谢。

   showAllRequest(parameter1){
  if (parameter1 === '') {
  /* call getmethod or the whole array variable got during get mehtod  
  */ 
  }
this.requests = this.requests.filter(request => request.requestedBy 
  === '*');
  }

coded version below : 下面的编码版本:

 filterform(search) {
 if (search === '') {
        this.getFormSettings();
       }
   this.requests = this.requests.filter(request => 
 request.requestedBy.indexOf(search.toLowerCase()) >= 0 === '*');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM