[英]create table with dynamic rows and columns with django html template
I'm trying to create a table with dynamic rows and columns based on the results list with django html template. 我正在尝试基于带有django html模板的结果列表创建具有动态行和列的表。 The number of records and header number could change. 记录数和标题数可以更改。 I am using two for loops to get the number of rows and the column number. 我使用两个for循环来获取行数和列数。 I'm having a hard time trying to output the actual values in the table. 我很难在表中输出实际值。 The idea was to "index" from the second for loop and applying it to "each" of first loop. 想法是从第二个for循环“索引”并将其应用于第一个循环的“每个”。 I know that the syntax is definitely wrong, but is something that django can do? 我知道语法绝对是错误的,但是django可以做到吗? If not, is there any suggestions that I can research on how to implement this? 如果没有,我是否可以研究如何实施此建议? Thanks!! 谢谢!!
list = [
{'header_a': foo1, 'header_b': foo2, 'header_c': foo3},
{'header_a': foo1, 'header_b': foo2, 'header_c': foo3},
{'header_a': foo3, 'header_b': foo3, 'header_c': foo3},
{'header_a': foo4, 'header_b': foo4, 'header_c': foo4},
]
Sample Table
header_a | header_b | header_c
foo1 | foo1 | foo1
foo2 | foo2 | foo2
foo3 | foo3 | foo3
foo4 | foo4 | foo4
or 要么
list_2 = [
{'header_c': c_foo1, 'header_d': d_foo1},
{'header_c': c_foo2, 'header_d': d_foo2},
]
Sample Table 2
header_c | header_d
c_foo1 | d_foo1
c_foo2 | d_foo2
<table>
<thead>
<tr>
{% for index in list.0 %}
<th>{{ index }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for each in list %}
<tr>
{% for index in a %}
<td>{{ each.{{index}} }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
maybe you should check out django tables ? 也许您应该查看Django表 ? It's a pretty known tool to creating powerful tables with django. 这是一个使用django创建功能强大的表的众所周知的工具。 You can create them based on a model, but you can also simply pass the data directly to them. 您可以基于模型创建它们,但也可以直接将数据直接传递给它们。 I also once used it to create a dynamic table. 我也曾经用它来创建动态表。 It takes all the job from the template and add it to the view where things are easier to manipulate. 它从模板中获取所有工作,并将其添加到易于操作的视图中。
Well, as you have a list of dicionaries in that schema, this is how you can iterate it inside the template: 好吧,由于您在该模式中有字典,因此可以在模板中进行迭代:
<table>
<thead>
<tr>
{% for item in list %}
{% if forloop.first %}
{% for h in item %}
<th>{{ h }}</th>
{% endfor %}
{% endif%}
{% endfor %}
</tr>
</thead>
<tbody>
{% for item in list %}
<tr>
{% for key,value in item.items %}
<td>{{ value}}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
Assuming you have the same keys in every dictionary (column headers or fields as you name them), you first iterate only the first dicionary to get its keys, then, you iterate again the values for rows... 假设每个字典中都有相同的键(列标题或字段名称),则首先仅迭代第一个字典以获取其键,然后再次迭代行的值...
Not the most efficient solution tho, it would be great if you can rewrite the way you create that list. 这不是最有效的解决方案,如果您可以重写创建列表的方式,那就太好了。
Hope it helps and please confirm if it works 希望对您有所帮助,请确认是否有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.