I am trying to use a custom pipe to filter my Json data loop using an input field with ngModel. Basically i have a side bar with large number of items so i want to have a input field so that i can search items in sidebar. Now the sidebar items can be single or may have sub-items. Here is my sidebar array (which basically a model for sidebar) from which i want to filter items :
Array
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"
}
]
},
];
Here is my Component
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;
}
Here is my View: View
<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>
I have not much experience with custom pipe i have searched on forums and created filter like this but it is not working
Custom Filter Pipe
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));
}
}
now what i need is that when i enter any term, my filter return me all those arrays (which eventually will be my list items) whose title matches with the search term
filter : {{searchText}}
(add curly braces). – anu start