2 回答
TA贡献1796条经验 获得超4个赞
柑橘朋克的答案有效,但这里有一些解释
HTML:
<input type="text"
[(ngModel)]="searchText" (ngModelChange)="searchTextChanged($event)" />
时间:
在 ngOnInit 之后 forEach 将 pubWorkspaces 存储在单独的数组中
workspaces.forEach(workspace => {
if(workspace.type == WorkspaceType.public){
this.pubWorkspaces.push(workspace);
}
});
this.filteredPubWorkSpaces= this.pubWorkspaces;
当搜索发生变化时,下面的函数会被调用,并且假设您在 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));
}
将其传递给组件
`<mc-workspace-card-list [workspaces] = "filteredPubWorkSpaces" [editable] = "false"></mc-workspace-card-list>`
TA贡献1155条经验 获得超0个赞
您可以制作一个显示结果的附加列表。只需在初始化组件时复制完整列表即可。在搜索栏更改事件中,您可以调用一个函数,该函数使用搜索词过滤原始列表并将其保存到额外的变量中:
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));
}
- 2 回答
- 0 关注
- 98 浏览
添加回答
举报