[英]render dynamic select option in html using javascript from json array
I have an object like this我有一个这样的对象
var Blocks = {
"name": "Column",
"properties": [
{
"name": "mainAxisAlignment",
"type":"dropDown",
"values":[
"center",
"end",
"spaceAround",
"spaceBetween",
"spaceEvenly",
"start"
]
},
{
"name": "crossAxisAlignment",
"type":"dropDown",
"values":[
"baseline",
"center",
"end",
"start",
"stretch"
]
},
{
"name":"direction",
"type": "string"
},
{
"name":"hashCode",
"type": "string"
},
{
"name":"key",
"type": "string"
},
{
"name": "mainAxisSize",
"type":"dropDown",
"values":[
"max",
"min"
]
},
{
"name":"textBaseline",
"type": "string"
},
],
}
I am trying render in html.我正在尝试在 html 中渲染。 so far I am able to get label and select list, but not able to get option in select list,到目前为止,我能够获得标签和选择列表,但无法在选择列表中获得选项,
This is what I tried so far.这是我到目前为止所尝试的。
document.getElementById('test1').innerHTML = `<div>
<div>${Blocks.name}</div>
<div id='selection'></div>
</div>`
document.getElementById('selection').innerHTML = Blocks.properties.map(user => {
switch (user.type) {
case 'dropDown':
return propertyDropdown(user)
case 'string':
return propertyString(user)
default:
break;
}
}).join('')
function propertyString(data) {
return `<div style="margin-left: 18px">
<label>${data.name}: </label>
</div>`
};
function propertyDropdown(data) {
return `<div style="margin-left: 18px">
<label for="property">${data.name}: </label>
<select id="property">
</select>
</div>`
};
function createOptions(option) {
var select = document.getElementById("property");
return option.map(item => {
return select.options[select.options.length] = new Option(item, item)
})
}
And This is the output I get这是我得到的输出
What I dont know is how do i use createOptions
function in this code.我不知道如何在这段代码中使用createOptions
函数。
You might use loop like:您可以使用如下循环:
for (key in data.values) {
output += '<option>' + data.values[key] + '</option>';
}
Example: https://jsfiddle.net/commanddotcom/j7f4y0kw/2/示例: https : //jsfiddle.net/commanddotcom/j7f4y0kw/2/
Your id
attributes will cause error since you are assigning the same ID for every dropdown.您的id
属性会导致错误,因为您为每个下拉列表分配了相同的ID。 You could use name
property for ID in this case.在这种情况下,您可以使用name
属性作为 ID。
var Blocks={name:"Column",properties:[{name:"mainAxisAlignment",type:"dropDown",values:["center","end","spaceAround","spaceBetween","spaceEvenly","start"]},{name:"crossAxisAlignment",type:"dropDown",values:["baseline","center","end","start","stretch"]},{name:"direction",type:"string"},{name:"hashCode",type:"string"},{name:"key",type:"string"},{name:"mainAxisSize",type:"dropDown",values:["max","min"]},{name:"textBaseline",type:"string"}]}; document.getElementById('test1').innerHTML = `<div> <div>${Blocks.name}</div> <div id='selection'></div> </div>` document.getElementById('selection').innerHTML = Blocks.properties.map(user => { switch (user.type) { case 'dropDown': return propertyDropdown(user) case 'string': return propertyString(user) default: break; } }).join('') function propertyString(data) { return `<div style="margin-left: 18px"> <label>${data.name}: </label> </div>` }; function propertyDropdown(data) { const options = data.values.map(opt => createOptions(opt)).join('') return `<div style="margin-left: 18px"> <label for="${data.name}">${data.name}: </label> <select id="${data.name}"> ${options} </select> </div>` }; function createOptions(option) { return `<option value="${option}">${option}</option>` }
<div id="test1"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.