【发布时间】:2016-12-09 23:22:10
【问题描述】:
根据访问网页的用户,生成的 HTML 表格可能会显示额外的列。我当前的实现检查模板文件中每一行的标志,其中show_secret_column 是 view 设置的标志:
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th class="col-md-4">Column 1 Header</th>
<th class="col-md-2">Column 2 Header</th>
<th class="col-md-2">Column 3 Header</th>
{% if show_secret_column %}
<th class="col-md-2">Secret Column Header</th>
{% endif %}
</tr>
</thead>
<tbody>
{% for row in row %}
<tr>
<td>{{ row.a }}</td>
<td>{{ row.b }}</td>
<td>{{ row.c }}</td>
{% if show_secret_column %}
<td>{{ row.secret }}</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
</div>
这是一个不好的方法吗?是否有任何其他方法建议在模板中只执行一次此检查并生成额外的列?
【问题讨论】:
-
这对我来说很好
-
@thedarklord47,我可以检查重复的
if条件是否执行得非常缓慢,导致随着行数的增加缩放不良?它“感觉”它正在放缓,我宁愿在两周后压力测试人员抱怨时不解决这个问题...... -
这个表应该有多少行?
-
@thedarklord47 我们网站现有版本中最大的表是 40k 行
-
我认为您正在尝试解决一个不存在的问题。除非您在使用此特定代码时遇到性能问题(不太可能 - 简单的布尔检查并不昂贵),否则请保持原样。
标签: html django django-templates html-table