YUI Menu:根据 json 对象的属性构建分层菜单
我有一个 json 对象,它具有类型和产品的属性。我正在寻找一种方法来循环遍历元素并使用 YUI 菜单将它们组织成分层菜单,以便第一级将按类型对它们进行分组,然后第二级菜单将按产品对它们进行分组。
json 看起来像这样:
[{ "productId":1, "typeId": 1, "productName": "test", "typeName": "old", "itemId": 1, "itemName": "item1"},
{ "productId":2, "typeId": 2, "productName": "test2", "typeName": "new", "itemId": 2, "itemName": "item2"},
{ "productId":2, "typeId": 1, "productName": "test2", "typeName": "old", "itemId": 3, "itemName": "item3"}]
我希望能够循环遍历项目并通过循环将它们添加到正确的子菜单中,但是 YUI 菜单的结构方式似乎不是一个简单的方法来做到这一点。由此产生的菜单结构将是这样的:
- 旧
- 测试
- 项目1
- 测试2
- 项目3
- 测试
- 新
- 测试2
- 项目2
2
- 测试2
问题澄清: 循环并创建单个项目非常简单:
for ( var i in obj )
{
menu.addItem(obj[i].itemName);
}
我需要做的是在子菜单不存在时循环创建子菜单并将项目附加到子菜单中。总而言之,可能有多达 200 个项目需要分类到这些子菜单中,因此每个分支都会有多个项目。我正在寻找一种简单的方法来执行检查/创建/附加工作流程。
I have a json object that has properties into a type and product. I am looking for a way to loop through the elements and organize them into a tiered menu using YUI menu so the first level would group them by type and then the second menu would group them by product.
The json looks something like:
[{ "productId":1, "typeId": 1, "productName": "test", "typeName": "old", "itemId": 1, "itemName": "item1"},
{ "productId":2, "typeId": 2, "productName": "test2", "typeName": "new", "itemId": 2, "itemName": "item2"},
{ "productId":2, "typeId": 1, "productName": "test2", "typeName": "old", "itemId": 3, "itemName": "item3"}]
I would like to be able to loop through the items and add them to their correct submenu through looping, but the way that YUI menu is structured, doesn't seem to be an easy way to do this. The resulting menu structure would be something like:
- old
- test
- item1
- test2
- item3
- test
- new
- test2
- item2
- test2
Question clarification:
Looping through and creating individual items is easy enough:
for ( var i in obj )
{
menu.addItem(obj[i].itemName);
}
What I need to do is to loop through creating submenus when they don't exists and appending the items to the submenus. All told there will be potentially up to like 200 items that will need to be sorted into these submenus so each branch will have several items. I'm looking for an easy way to do the check/create/append workflow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在尝试创建列表之前,请按以下顺序对 JSON 数据结构进行稳定排序:
这将按照与执行深度优先搜索相同的顺序排列数据。树。 在哪里
测试
测试
变为
注意,第一列或第二列中的更改表示必须(分别)创建新类别或子类别。
与这个问题无关,您可能需要考虑使用不同的方法来显示数据。从用户的角度来看,滚动浏览一系列包含 200 多个项目的分层下拉菜单肯定会很痛苦。
Before attempting to create the list, sort the JSON data structure with a stable sort in the following order:
This will arrange the data in the same order as if you performed a depth-first search of the tree. Where
becomes
Note that a change in the first or second column indicates that a new category or subcategory (respectively) must be created.
Unrelated to this question, you might want to consider a different method of displaying the data. From a user perspective, scrolling through a series of hierarchical drop-down menus that contains 200+ items is bound to be painful.