【发布时间】:2013-12-29 17:00:46
【问题描述】:
我正在尝试遍历传递到 Jinja2 模板中的一些数据并填充表格。
我想将每个<td> 单元格拆分为更多子列如果有更多信息,<td> 中的子列数是动态的。
示例 1
我希望每个<td> 产生这样的结果(即,无论每个<td> 中有多少子列,新的子列都将显示在同一行中):
@987654321 @
示例2##
在此示例中,<td> 中的每个新子列都显示在前一个子列下方(我不希望这样):
如果我使用这个小 sn-p 代码并在浏览器中打开它,它会按我的意愿工作(示例 1)。
<html>
<head>
<style type="text/css">
.cell {
display: block;
}
.col {
float:left;
/*display: inline;*/
}
</style>
</head>
<body>
<table style="border:solid">
<thead>
<tr><th>Example 1</th>
<th>Example 2</th>
<th>Example 3</th></tr>
</thead>
<tr>
<td>
<div class="cell">
<div class='col'>
item 1 <br />
item 2 <br />
item 3 <br />
</div>
<div class='col'>
item 1 <br />
item 2 <br />
item 3 <br />
</div>
<div class='col'>
item 1 <br />
item 2 <br />
item 3 <br />
</div>
</div>
</td>
<td>
<div class="cell">
<div class='col'>
item 1 <br />
item 2 <br />
item 3 <br />
</div>
</div>
</td>
<td>
<div class="cell">
<div class='col'>
item 1 <br />
item 2 <br />
item 3 <br />
</div>
<div class='col'>
item 1 <br />
item 2 <br />
item 3 <br />
</div>
</div>
</td>
</tr>
</table>
</body>
</html>
但是它在模板中没有按预期工作。如果我使用相同的样式,<td> 不会扩大以适应新的子列。 <td> 确实会扩大以容纳每列中的文本。
<table>
<thead>Some information about all the cells</thead>
{% for person in list_of_people%}
<tr>
<td>
<div class="cell">
{% for item in person.activity %}
<div class='col'>
{% for element in item %}
{{ loop.index }}
{% if element['correct'] == True %}
✔
{% else %}
✗
{% endif %}
<br />
{% endfor %}
SCORE <br />= {{stats[0]}} / {{stats[2]}}
<br />
</div> <!-- col -->
{% endfor %}
</div> <!-- attempt -->
</td>
</tr>
{% endfor %}
</table>
我如何确保每个<td> 中的每个<div> 保持在同一行并且不会被推到前一行以下?
【问题讨论】:
-
你为什么用
divs来显示tabular数据? -
你很可能通过
display: table-cell到CSS来实现这一点……但话说回来,你为什么不使用表格。 -
我使用的是
<table>,但在表格中我想根据是否有更多信息可以将<td>分成更多的列...