如何从JSON创建下拉菜单

时间:2016-07-04 06:01:29

标签: javascript html css json

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

以下是我的JSON菜单结构:

Screenshot

[
        {"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;
}

1 个答案:

答案 0 :(得分:-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);
    };

for refrence https://plnkr.co/edit/FZNFuu1G3KAvPRvPGtqV?p=preview