I am currently working on an Angular app and facing a challenge related to the navigation menu. The menu includes a search option for filtering. Below is the JSON structure for the menu:
menuItems: [
{
name: "Dashboard",
iconPath: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="105471637850727f7162743e607e77">[email protected]</a>",
routingPath: "",
children: [
{
name: "Version 1",
iconPath: "",
routingPath: "",
children: [
{
name: "Version 1.1 abc",
iconPath: "",
routingPath: "",
children: [
{
name: "Version 1.1.1 ccc",
iconPath: "",
routingPath: "",
children: [
{
name: "Version 1.1.1.1 hello",
iconPath: "",
routingPath: "",
children: []
},
...
This example demonstrates the menu structure. My goal is to display specific menu items based on a search query like "hello," showcasing all relevant data within the object:
https://i.sstatic.net/41chQ.jpg
However, my current solution is not yielding the desired outcome. I've attempted the following code:
private performFiltering() {
const searchValue: string = this.searchBarInput.nativeElement.value.toLowerCase();
if (searchValue.length > 0) {
...
}
private recursiveSearchInMenu(menu: MenuItemModel, searchValue) {
let found = menu.children.find((item) => item.name.toLowerCase().includes(searchValue));
...
}
The outcome of my implementation can be seen here: https://i.sstatic.net/qaTKr.jpg
If you have any suggestions or ideas on how to improve this search functionality, please share them. Thank you!