【发布时间】:2014-02-18 14:43:18
【问题描述】:
提前感谢您抽出宝贵时间提供帮助。我在 node.js 中使用 Jade 模板引擎,并希望生成一个 HTML 表,其中的行分组为部分(tbody 标记)。
假设我在内存中有以下对象:
[
{ type: 'Fruit', name: 'Apple' }
, { type: 'Fruit', name: 'Orange' }
, { type: 'Vegetable', name: 'Carrot' }
, { type: 'Vegetable', name: 'Spinach'}
]
(为简单起见,我们假设数组按“类型”列预先排序)。
并且我想为每个“类型”(水果与蔬菜)的 tbody 部分内的每个对象生成一个包含行的表。所以我要生成的 HTML 是:
<table>
<thead>
<th>Type</th>
<th>Name</th>
</thead>
<tbody id="Fruit">
<tr>
<td>Fruit</td>
<td>Apple</td>
</tr>
<tr>
<td>Fruit</td>
<td>Orange</td>
</tr>
</tbody>
<tbody id="Vegetable">
<tr>
<td>Vegetable</td>
<td>Carrot</td>
</tr>
<tr>
<td>Vegetable</td>
<td>Spinach</td>
</tr>
</tbody>
</table>
我想我希望我的玉看起来像:
table
thead
th Type
th Name
- var lastType;
each o in objs
- if(o.type != lastType)
tbody(id='#{o.type}')
- lastType = o.type;
tr
td #{o.type}
td #{o.name}
但这会产生:
<table>
<thead>
<th>Type</th>
<th>Name</th>
</thead>
<tbody id="Fruit" />
<tbody>
<tr>
<td>Fruit</td>
<td>Apple</td>
</tr>
<tr>
<td>Fruit</td>
<td>Orange</td>
</tr>
</tbody>
<tbody id="Vegetable" />
<tbody>
<tr>
<td>Vegetable</td>
<td>Carrot</td>
</tr>
<tr>
<td>Vegetable</td>
<td>Spinach</td>
</tr>
</tbody>
</table>
有什么想法吗?
【问题讨论】:
标签: node.js html-table pug