我有一个由单选按钮组成的大表单,我想用nunjucks动态创建。
我有一个带有数据的json文件,用变量填充每个html表单输入组。 html包含每组两个无线电输入。
我可以从json文件中检索变量但是仍然坚持创建FOR循环。
我想要实现的是循环遍历checklist.json中的每个子部分,并使用每个数组中的变量填充html列表,构建列表直到数据结束。
从html中可以看出,除了value之外,每个数组中的所有变量都在html输入块中使用了两次。
总结:只要有包含数组的子部分,就迭代html表单输入并用每个数组中的对象填充每个部分。
index.njks
{% include "../includes/checklist-radio.njk" %}
checklist.json(var = checklist_json)
{"checklist_title":"checklist variable test",
"checklist": [
{"section_title":"Responsive Design (Useability)",
"section":[
{"subsection_title1":"Mozilla Firefox Useability",
"subsection":[
{
"radio_title": "Mobile (Samsung S7 Edge)",
"name":"firefox_mobile",
"value": 1,
"class":"useability"
},
{
"radio_title": "Tablet (Surface Pro)",
"name":"firefox_tablet",
"value": 1,
"class":"useability"
},
{
"radio_title": "Desktop (Windows 10)",
"name":"firefox_desktop",
"value": 1,
"class":"useability"
}
]}
]}
]}
清单-radio.njk
{% for checklist_json.checklist.section.subsection in checklist_json.checklist.section %}
<li>
<span class="radio_title">{{checklist_json.checklist.section.subsection.radio_title}}</span>
<label class="radio_label">
<input type="radio" class="radio {{checklist_json.checklist.section.subsection.class}}" name="{{checklist_json.checklist.section.subsection.name}}" value="{{checklist_json.checklist.section.subsection.value | escape}}">
Yes</label>
<label class="radio_label">
<input type="radio" class="radio {{checklist_json.checklist.section.subsection.class}}" name="{{checklist_json.checklist.section.subsection.name}}" value="0">
No</label>
</li>
{% endfor %}
请问?
编辑:许多部分和子部分的真实列表要大得多。
答案 0 :(得分:1)
你试图走错路。查看此代码以获取第一部分的第一小节:
checklist_json.checklist[0].section[0].subsection
您是否看到该清单是数据谁关心部分数组谁保留子部分对象与您需要的数据。试着走这条路。我会尝试这样做,但这是我第一次见到这个图书馆。
我将在jsfiddle link过去,稍后尝试解决。
UPD1:我们需要嵌套for
{% for checklist in checklist_json %}\
{% for section in checklist %}\
{% for subsection in section %}\
我会继续尝试
Ough。我知道了。 Check my example here。我缩短代码以显示您需要做的主要更改。如果您需要进一步的解释或代码重写,请给我一句话。 或者进一步more code example。 有一个很好的编码!
nunjucks.configure({ autoescape: false });
var checklist_json = {
"checklist_title": "checklist variable test",
"checklist": [
{
"section_title": "Responsive Design (Useability)",
"section": [
{
"subsection_title1": "Mozilla Firefox Useability",
"subsection": [
{
"radio_title": "Mobile (Samsung S7 Edge)",
"name": "firefox_mobile",
"value": 1,
"class": "useability"
},
{
"radio_title": "Tablet (Surface Pro)",
"name": "firefox_tablet",
"value": 1,
"class": "useability"
},
{
"radio_title": "Desktop (Windows 10)",
"name": "firefox_desktop",
"value": 1,
"class": "useability"
}
]
}
]
}
]
};
document.body.innerHTML = nunjucks.renderString(""
+ "<h1>{{ checklist_title }}</h1>"
+ "{% for checklist in checklist %}"
+ "<h2>{{ checklist.section_title }}</h2>"
+ "{% for section in checklist.section %}"
+ "<h3>{{ section.subsection_title1 }}</h3>"
+ "{% for subsection in section.subsection %}"
+ "<li>"
+ "<span class='radio_title'>"
+ "{{subsection.radio_title}}"
+ "</span>"
+ "<label>"
+ "<input type='radio' "
+ "class='radio {{subsection.class}}' "
+ "name='{{subsection.name}}' "
+ "value='{{subsection.value | escape}}'>"
+ "Yes"
+ "</label>"
+ "<label>"
+ "<input type='radio' "
+ "class='radio {{subsection.class}}' "
+ "name='{{subsection.name}}' "
+ "value='0'>"
+ "No"
+ "</label>"
+ "</li>"
+ "{% endfor %}"
+ "{% endfor %}"
+ "{% endfor %}", checklist_json);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/nunjucks/3.0.0/nunjucks.js"></script>
&#13;