繁体   English   中英

如何从JSON创建下拉菜单

[英]How to create a dropdown menu from a JSON

是否有人已经创建或完成了基于JSON结构创建菜单和下拉菜单的编程。

这是我的JSON菜单结构,如下所示:

屏幕截图

[
        {"root" : "Input", 
         "subs" : [
                  {"sub_name": "CSV Reader","type": "csv",
                     "subs_level2": [{"sub_name": "Reader 1","type": "csv"},
                                     {"sub_name": "Reader 2","type": "csv"}
                                     ]
                  },
                  {"sub_name": "DB Connect","type": "db"},
                  {"sub_name": "Sample Data","type": "sample data"}
                  ]
        },
        {"root" : "Output", 
         "subs" : [
                  {"sub_name": "Output 1","type": "output1"},
                  {"sub_name": "Output 2","type": "output2"},
                  {"sub_name": "Output 3","type": "output3"}
                  ]
        }
     ]

这是我最初的javascript:

// setup the main div
//<div id="canvas_dock"></div>
//$('#canvas_dock').append("<div class='node_dropdown'>
//<span>Input (Root)</span>
//<div class='node_dropdown-content'>
//<div id="">CSV Reader</div>
//<div id="">DB Connect</div>
//<div id="">Sample Data</div>
//</div>
//</div>");

//here is the loop
var key = "root", idx = 0;
for(key in nodeTypes){
    if(nodeTypes[idx].root != undefined && nodeTypes[idx].root == root_menu){
      for(var sub in nodeTypes[idx].subs){
        $('#node_dropdown-content').append(nodeTypes[idx].subs[sub].sub_name);
      }
    }
    idx = idx + 1;
}

嗨,用JavaScript检查一下

 var JSON =[
    {
        "root": "Input",
        "subs": [
            {
                "sub_name": "CSV Reader",
                "type": "csv",
                "subs_level2": [
                    {
                        "sub_name": "Reader 1",
                        "type": "csv"
                    },
                    {
                        "sub_name": "Reader 2",
                        "type": "csv"
                    }
                ]
            },
            {
                "sub_name": "DB Connect",
                "type": "db"
            },
            {
                "sub_name": "Sample Data",
                "type": "sample data"
            }
        ]
    },
    {
        "root": "Output",
        "subs": [
            {
                "sub_name": "Output 1",
                "type": "output1"
            },
            {
                "sub_name": "Output 2",
                "type": "output2"
            },
            {
                "sub_name": "Output 3",
                "type": "output3"
            }
        ]
    }
]


    var select = document.getElementById("selector");
    for (var i = 0; i < JSON[0].subs.length; i++) {
        var option = document.createElement("option");
        option.value = i;
        option.textContent = JSON[0].subs[i].sub_name;
        select.appendChild(option);
    };

推荐https://plnkr.co/edit/FZNFuu1G3KAvPRvPGtqV?p=preview

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM