简体   繁体   English

如何以角度过滤垫卡

[英]How to filter mat card in angular

I'm wondering how I can use the search bar( https://stackblitz.com/edit/stackoverflowcom-a-60857864-6433166-dpaump ) and the code below to be able to filter the cards using it name.我想知道如何使用搜索栏( https://stackblitz.com/edit/stackoverflowcom-a-60857864-6433166-dpaump )和下面的代码来使用它的名称过滤卡片。

This is my Explore TS file (I imported matcards and routers components from different files)这是我的 Explore TS 文件(我从不同的文件中导入了 matcards 和 routers 组件)

import { Router } from '@angular/router';
import { WorkspaceService } from 'src/app/core/services/workspace.service';
import { Workspace, WorkspaceType } from 'src/app/shared/models/workspace.model';

And this is inside HTML file to display the cards这是在 HTML 文件中显示卡片

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

And this is what it look like right now这就是它现在的样子

[![enter image description here][1]][1] [![在此处输入图像描述][1]][1]

Note: I did not included the code above in my Stackblitz file because there are alot of components involved and I'm wondering if someone can help me or give me tips on how to implement this filter function for cards by just looking the code above and stackblitz file.注意:我没有在我的 Stackblitz 文件中包含上面的代码,因为涉及到很多组件,我想知道是否有人可以帮助我或通过查看上面的代码给我有关如何为卡片实现此过滤器功能的提示和stackblitz 文件。

Citrus punk's answer works but here is some explanation柑橘朋克的答案有效,但这里有一些解释

HTML: HTML:

<input type="text"
[(ngModel)]="searchText" (ngModelChange)="searchTextChanged($event)" />

Ts: :

In ngOnInit After this forEach store copy pubWorkspaces in separate array在 ngOnInit 这个 forEach 之后,将 pubWorkspaces 复制到单独的数组中

   workspaces.forEach(workspace => {
        if(workspace.type == WorkspaceType.public){
          this.pubWorkspaces.push(workspace);
        }
      });
   this.filteredPubWorkSpaces= this.pubWorkspaces;

Below function gets called when there is change in search and im assuming you have name property in pubws object.当搜索发生变化并且我假设您在 pubws 对象中有 name 属性时,将调用下面的函数。

searchTextChanged(searchText){
 //return array of workspace only having search text in their names
  this.filteredPubWorkSpaces= this.pubWorkspaces.filter(pubws=>pubws.name.includes(searchText));
}

Pass it to component传递给组件

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

You could make an additional list that is showing the results.您可以制作一个显示结果的附加列表。 Just copy the full list when initializing the component.初始化组件时只需复制完整列表。 On the searchbar change event you can call a function that filters the original list with the searchterm and save this to an extra variable:在搜索栏更改事件中,您可以调用一个函数,该函数使用搜索词过滤原始列表并将其保存到一个额外的变量中:

export class ExploreComponent implements OnInit, OnDestroy {

private workspaces;
public filteredWorkspaces;
public searchterm;

constructor(private workspaceService: WorkspaceService, private router: Router) { }


ngOnInit(): void {

    this.loading = true;
    this.workspaceService.getUserWorkspaces().subscribe((workspaces) =>{

    workspaces.forEach(workspace => {
      if(workspace.type == WorkspaceType.public){
        this.pubWorkspaces.push(workspace);
      }
    this.filteredWorkspaces = workspaces;
}

onInputChange(){
    this.filteredWorkspaces = this.workspaces.filter(ws => ws.name.includes(this.searchterm));
}

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

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