【问题标题】:Angular filter not returning back full list after search搜索后角度过滤器不返回完整列表
【发布时间】:2020-07-28 22:32:21
【问题描述】:

我有一个过滤垫卡的搜索框,但是当用户完成搜索时,所有垫卡都不再显示。

例如,我有一堆卡片,用户搜索“John Card”,它会过滤 John Card,但是当用户删除他的输入时,只有 John Card 卡在那里,并且不再显示完整的垫子卡片。

TS 文件

  searchText:string;
  workspaces: Workspace[] = [];

    this.loading = true;
    this.workspaceService.getPublicWorkspaces().pipe(takeUntil(this.death$)).subscribe((workspaces) =>{

    this.workspaces = workspaces;

    this.showColumn = false;
    this.loading = false;
    }, ()=>this.loading = false)
}
  searchTextChanged(searchText){
    //return array of workspace only having search text in their names
     this.workspaces = this.workspaces.filter(pubws=>pubws.name.toLowerCase().includes(searchText.toLowerCase().trim()));
   }

HTML

<input matInput [formControl]="inputCtrl" [(ngModel)]="searchText" (ngModelChange)="searchTextChanged($event)" placeholder="Search" class="input">

<mc-workspace-card-list [workspaces] = "workspaces" [editable] = "false"></mc-workspace-card-list>

任何建议。

【问题讨论】:

    标签: angular search filter angular-material filtering


    【解决方案1】:

    因为你在这里覆盖了你的模型

    this.workspaces = this.workspaces.filter(pubws=>pubws.name.toLowerCase().includes(searchText.toLowerCase().trim()));
    

    在 html 中将您的输入更改为

    <input matInput [formControl]="inputCtrl" [(ngModel)]="searchText" (ngModelChange)="searchTextChanged()" placeholder="Search" class="input">
    

    在声明后初始化你的字符串

    searchText:string="";
    

    再创建一个 temp_element

    workspacesTemp: Workspace[] = [];
    

    两个都填

     this.workspaces = workspaces;
     this.workspacesTemp = workspaces;
    

    用这个温度过滤它

    searchTextChanged(){
        var search=this.searchText;
        if(search==""){this.workspaces=this.workspacesTemp;}
         this.workspaces = this.workspacesTemp.filter(pubws=>pubws.name.toLowerCase().includes(search.toLowerCase().trim()));
       }
    

    【讨论】:

    • 感谢您,但出于某种原因,它不再过滤。我还在 HTML 中同时更改了工作空间和 wospacesTemp,但它不再过滤了。
    • 当您从服务获取数据时,您是否也填写了 workspacesTemp?
    • 你能在你的问题中分享你的所有组件然后我可以看到问题部分吗?
    • 我不确定你的意思,但我确实按照你告诉我的做了,遗憾的是它不再过滤了。如果您不介意检查,我会编辑我的帖子并包含整个 Typescript 代码。
    • 我编辑了我的答案,由于我的答案你可以再次编辑?
    猜你喜欢
    • 2014-11-29
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多