YUI Menu:根据 json 对象的属性构建分层菜单

发布于 2024-08-03 07:43:11 字数 965 浏览 2 评论 0原文

我有一个 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

问题澄清: 循环并创建单个项目非常简单:

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
  • new
    • test2
      • item2

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话墙 2024-08-10 07:43:11

在尝试创建列表之前,请按以下顺序对 JSON 数据结构进行稳定排序:

  1. 项目名称(可选)
  2. 产品名称
  3. 类型名称

这将按照与执行深度优先搜索相同的顺序排列数据。树。 在哪里

    • 测试 1
      • 第 3 项
      • 第 1 项
    • 测试 2
      • 第 2 项
    • 测试

    • 测试 2
      • 第 4 项
    • 测试

变为

Old > Test 1 > Item 1
Old > Test 1 > Item 3
Old > Test 2 > Item 2
New > Test 2 > Item 4

注意,第一列或第二列中的更改表示必须(分别)创建新类别或子类别。

与这个问题无关,您可能需要考虑使用不同的方法来显示数据。从用户的角度来看,滚动浏览一系列包含 200 多个项目的分层下拉菜单肯定会很痛苦。

Before attempting to create the list, sort the JSON data structure with a stable sort in the following order:

  1. Item Name (optional)
  2. Product Name
  3. Type Name

This will arrange the data in the same order as if you performed a depth-first search of the tree. Where

  • Old
    • Test 1
      • Item 3
      • Item 1
    • Test 2
      • Item 2
  • New
    • Test 2
      • Item 4

becomes

Old > Test 1 > Item 1
Old > Test 1 > Item 3
Old > Test 2 > Item 2
New > Test 2 > Item 4

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文