【发布时间】:2019-10-07 14:12:18
【问题描述】:
我正在开发一个 Angular 应用程序。 我的问题是这样的: 我在侧面有一个导航菜单,可以在菜单内进行搜索。 这是菜单的 JSON:
menuItems: [
{
name: "Dashboard",
iconPath: "Dash@board.png",
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: []
},
{
name: "Version 1.1.2.1",
iconPath: "",
routingPath: "",
children: []
},
{
name: "Version 1.1.3.1 sdfsdf",
iconPath: "",
routingPath: "",
children: []
}
]
},
{
name: "Version 1.1.2",
iconPath: "",
routingPath: "",
children: []
},
{
name: "Version 1.1.3",
iconPath: "",
routingPath: "",
children: []
}
]
},
{
name: "Version 1.2",
iconPath: "",
routingPath: "",
children: []
}
]
},
{
name: "Version 2",
iconPath: "",
routingPath: "",
children: []
},
{
name: "Version 3",
iconPath: "",
routingPath: "",
children: [
{
name: "Version 3.1",
iconPath: "",
routingPath: "",
children: []
},
{
name: "Version 3.2",
iconPath: "",
routingPath: "",
children: []
},
{
name: "Version 3.3",
iconPath: "",
routingPath: "",
children: []
}
]
}
]
},
{
name: "Pages",
iconPath: "Pa@ges.png",
routingPath: "",
children: [
{
name: "ZZZ Page 1",
iconPath: "",
routingPath: "",
children: []
}
]
},]
这是一个菜单示例。 我想要实现的是: 如果有人搜索你好,我需要显示这个(我只显示名称,但我需要对象内的所有数据):
但它不起作用。 我试过这段代码:
private performFiltering() {
const searchValue: string = this.searchBarInput.nativeElement.value.toLowerCase();
if (searchValue.length > 0) {
this.filteredMenu.menuItems = this.fullMenu.menuItems.filter((item) => {
return this.recursiveSearchInMenu(item, searchValue);
});
}}
private recursiveSearchInMenu(menu: MenuItemModel, searchValue) {
let found = menu.children.find((item) => item.name.toLowerCase().includes(searchValue));
if (!found) {
let i = 0;
while (!found && i < menu.children.length) {
if (menu.children[i].children && menu.children[i].children.length) {
found = this.recursiveSearchInMenu(menu.children[i], searchValue);
}
i++;
}
}
return found;
}
任何想法如何做到这一点?
【问题讨论】:
标签: angular typescript recursion search filter