简体   繁体   English

使用Django HTML模板创建具有动态行和列的表

[英]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.

相关问题 Angular 6:HTML 表格创建动态列和行 - Angular 6: HTML table create dynamic columns and rows 绘制具有动态列/行的 HTML 表 - Drawing HTML table with dynamic columns/rows 创建动态表-N列3列? - Create dynamic table - 3 Columns by N-rows? 使用JS在html中创建具有动态行和列的表 - Creat table with dynamic rows and columns in a html using JS 如何使用jQuery和乘法行和列创建HTML表 - How to create HTML table with jQuery and multiplication rows and columns 创建动态表:具有标题/标签的部分-其相关的动态行,列,然后类似地再次显示另一部分 - Create dynamic table: a section with a heading/label - its related dynamic rows, columns and then again another section similarly 如何在 php、html、脚本中创建 3 行和无限列的动态表? 我还需要在每个字段中都有一个链接到站点的按钮 - How can I create dynamic table with 3 rows and unlimited columns in php, html, script? I also need a button in each field which links to a site 我想创建HTML页面,该页面需要通过按行和列数提供输入来显示表的行和列 - I want to create HTML page which needs to show table rows and column by giving inputs by number of rows and columns React.js 创建具有动态列和动态行的动态表 - React.js Creating a Dynamic table with Dynamic Columns and Dynamic Rows 在angularjs中以行和列创建动态复选框按钮 - Create dynamic checkbox buttons in angularjs in rows and columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM