英文:
Is there a way to get the values of outer loops when using nested loops in html?
问题
目前弹出窗口的ID重复使用,并且“编辑”按钮仅在第一个“项目”内起作用。我想要使用外部循环的值来创建弹出窗口的唯一ID。有更好的方法吗?
当前布局列出了所有项目,每个项目内列出了所有订单,每个订单内列出了所有行项目。
示例:
项目名称
--> 订单编号
--> 行项目
--> 行项目
--> 订单编号
--> 行项目
--> 行项目
以下是我的代码:
{% for project in projects %}
...
{% for order in project.orders %}
...
{% for lineItem in order.line_items %}
<td>
<button onclick="editRow(this, '{{ loop.index }}'), openPopup('{{ loop.index }}')" class="remove-button" style="background: #00e50b">编辑</button>
<div class="popup" id="popup{{ loop.index }}">
<div class="popup-content">
<h2 style="color: #000000">编辑新的预计到达时间/备注</h2>
<form action="/manage_stock" method="POST" style="display: none;">
...
</form>
</div>
</div>
</td>
{% endfor %}
...
{% endfor %}
...
{% endfor %}
英文:
Currently the id of the popup is used repeatedly and the Edit button only works inside the first "project" . I would like to use the values of the outer loops as well to create a unique id for the popup. Are there better ways to do this?
The current layout lists all projects inside each project all the orders and inside each order the line items.
Example:
PROJECT NAME
--> ORDER NUMBER
--> LINE ITEM
--> LINE ITEM
--> ORDER NUMBER
--> LINE ITEM
--> LINE ITEM
Here is my code:
{% for project in projects %}
...
{% for order in project.orders %}
...
{% for lineItem in order.line_items %}
<td>
<button onclick="editRow(this, '{{ loop.index }}'), openPopup('{{ loop.index }}')" class="remove-button" style="background: #00e50b">Edit</button>
<div class="popup" id="popup{{ loop.index }}">
<div class="popup-content">
<h2 style='color: #000000'>Edit New ETA / Comments</h2>
<form action="/manage_stock" method="POST" style="display: none;">
...
</form>
</div>
</div>
</td>
{% endfor %}
...
{% endfor %}
...
{% endfor %}
答案1
得分: 2
你可以使用 loop.parent.loop.index
和/或 loop.parent.loop.parent.loop.index
来访问 Twig 中外部循环的索引。
{% for project in projects %}
{% set project_index = loop.index %}
{% for order in project.orders %}
{% set order_index = loop.index %}
{% for lineItem in order.line_items %}
复合索引:{{ project_index }}-{{ order_index }}-{{ loop.index }}
{% endfor %}
{% endfor %}
{% endfor %}
英文:
You can use loop.parent.loop.index
and/or loop.parent.loop.parent.loop.index
to access to outer loop indexes for Twig
<h3>EDIT</h3>
So, if you are using Jinja and it doesn't support parent loop index, you can try to save it in a variable:
{% for project in projects %}
{% set project_index = loop.index %}
{% for order in project.orders %}
{% set order_index = loop.index %}
{% for lineItem in order.line_items %}
Composite index: {{ project_index }}-{{ order_index }}-{{ loop.index }}
{% endfor %}
{% endfor %}
{% endfor %}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论