我正在尝试使用自定义管道使用带有ngModel的输入字段来过滤我的Json数据循环。 基本上我有一个带有大量项目的侧栏,所以我想有一个输入字段,以便我可以搜索侧栏中的项目。 现在侧边栏项目可以是单个项目,也可以包含子项目。 这是我的侧边栏数组(基本上是侧边栏的模型),我想从中过滤项目:
阵列
export const menu_items = [
{
"title": "Dashboard",
"class": "nav-item",
"iconClass": "icon-speedometer"
},
{
"title": "Configuration",
"class": "nav-item nav-dropdown",
children: [
{
"title": "Area Setup",
"iconClass": "icon-puzzle",
"dropdown" : true,
children: [
{
"title": "Company",
"iconClass": "icon-puzzle"
},
{
"title": "Office",
"iconClass": "icon-puzzle"
}
]
},
{
"title": "User Setup",
"iconClass": "icon-puzzle",
"dropdown" : true,
children: [
{
"title": "Company",
"iconClass": "icon-puzzle"
},
{
"title": "Profile",
"iconClass": "icon-puzzle"
},
{
"title": "User",
"iconClass": "icon-puzzle"
},
{
"title": "Role",
"iconClass": "icon-puzzle"
}
]
},
]
},
{
"title": "Receiving",
"class": "nav-item",
children: [
{
"title": "Receipt Enquiry",
"iconClass": "icon-speedometer"
},
{
"title": "Receiving",
"iconClass": "icon-speedometer"
},
]
},
{
"title": "Logs",
"class": "nav-item",
children: [
{
"title": "Inventory Logs",
"iconClass": "icon-list"
},
{
"title": "Audit Logs",
"iconClass": "icon-list"
},
{
"title": "Process Logs",
"iconClass": "icon-list"
}
]
},
];
这是我的组件
import { MenuModel, menu_items } from "./menu.model";
@Component({
selector: 'app-dashboard',
templateUrl: './layout.component.html',
providers: [MenuModel]
})
export class LayoutComponent {
searchText = '';
public menu_items = menu_items;
}
这是我的观点: 查看
<input type="text" class="form-control" [(ngModel)]="searchText" placeholder="Search in menu">
<div class="sidebar">
<nav class="sidebar-nav">
<ul *ngFor="let menu of menu_items | filter : searchText" class="nav" >
<li class="nav-title text-center">
<span>{{menu.title}}</span>
</li>
<li *ngFor="let child of menu.children" class="{{menu.class}}" >
<a *ngIf="!child.component" class="nav-link" href="javascript:void(0)" > <i class="{{child.iconClass}}"> </i>
{{child.title}}
<span *ngIf="child.new" class="badge badge-info">NEW </span>
</a>
<ul *ngIf="child.children" class="nav-dropdown-items">
<li class="nav-item" *ngFor="let childlevel2 of child.children">
<a class="nav-link" href="javascript:void(0)"
<i class="{{childlevel2.iconClass}}"></i>
{{childlevel2.title}}
</a>
</li>
</ul>
</li>
</ul>
</nav>
</div>
我对自定义管道的经验不多,我在论坛上搜索并创建了这样的过滤器,但它无法正常工作
自定义过滤管道
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'filter'
})
export class FilterPipe implements PipeTransform {
transform(items: any[], searchText: string): any[] {
if(!items) return [];
if(!searchText) return items;
searchText = searchText.toLowerCase();
return items.filter(item => item.title.includes(searchText));
}
}
现在我需要的是,当我输入任何术语时,我的过滤器会返回所有那些标题与搜索词匹配的数组(最终将是我的列表项)
答案 0 :(得分:-2)
您是否尝试运行:##过滤器:{{searchText}} ##