JPB*_*JPB 7 javascript jinja2 nunjucks
我有一个由单选按钮组成的大型表单,我想用nunjucks动态创建.
我有一个带有数据的json文件,用变量填充每个html表单输入组.html包含每组两个无线电输入.
我可以从json文件中检索变量,但我仍然坚持创建FOR循环.
我想要实现的是循环遍历checklist.json中的每个子部分,并使用每个数组中的变量填充html列表,构建列表直到数据结束.
从html中可以看出,除了value之外,每个数组中的所有变量都在html输入块中使用了两次.
简介:只要有包含数组的子节,就迭代html表单输入并用每个数组中的对象填充每个输入.
index.njks
{% include "../includes/checklist-radio.njk" %}
Run Code Online (Sandbox Code Playgroud)
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"
}
]}
]}
]}
Run Code Online (Sandbox Code Playgroud)
清单-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 %}
Run Code Online (Sandbox Code Playgroud)
请问好吗?
编辑:许多部分和子部分的实际列表要大得多.
你试图走错路。查看此代码以获取第一部分的第一小节:
checklist_json.checklist[0].section[0].subsection
Run Code Online (Sandbox Code Playgroud)
您是否看到清单是关心部分的数组,其中保存了包含您需要的数据的小节对象。尝试沿着这条路走。我会尝试这样做,但这是我第一次见到这个图书馆。
我将在此处传递jsfiddle 链接,并稍后尝试解决。
UPD1:看起来我们需要for嵌套
{% for checklist in checklist_json %}\
{% for section in checklist %}\
{% for subsection in section %}\
Run Code Online (Sandbox Code Playgroud)
我会继续努力
哎哟。我得到了它。在这里查看我的示例。我使代码更短以显示您需要做的主要更改。如果您需要进一步的解释或代码重写,请给我一行。或者更多代码示例。祝你编码愉快!
checklist_json.checklist[0].section[0].subsection
Run Code Online (Sandbox Code Playgroud)
{% for checklist in checklist_json %}\
{% for section in checklist %}\
{% for subsection in section %}\
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
530 次 |
| 最近记录: |