我可以迭代小胡子渲染的对象吗?
Can I iterate object rendered by mustache?
我有 table 由小胡子循环生成,如下所示:
names.mustache
<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th>
#
</th>
<th>
Name
</th>
</tr>
</thead>
<tbody>
{{#allnames}}
{{#active}}
<tr>
<td>
{{count}}
</td>
<td>
{{name}}
</td>
</tr>
{{/active}}
{{/allnames}}
</tbody>
</table>
我想迭代 count 以便我的 table 可以有行号。基本上,我初始化了 $count=1
。我怎样才能用干净高效的代码来实现它?还是有更好更简单的方法?
已更新
对于 Mustache,您需要创建一个函数
var data = {
items: allnames,
, index: function() {
return ++window['index']||(window['index']=0);
}
}
然后在循环中使用{{index}}
原本
抱歉,我正在考虑如下所示的车把(请参阅上面的正确答案)
使用{{@index}}
{{#allnames}}
{{name}} is {{@index}}
{{/allnames}}
注意:索引从零开始
我有 table 由小胡子循环生成,如下所示:
names.mustache
<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th>
#
</th>
<th>
Name
</th>
</tr>
</thead>
<tbody>
{{#allnames}}
{{#active}}
<tr>
<td>
{{count}}
</td>
<td>
{{name}}
</td>
</tr>
{{/active}}
{{/allnames}}
</tbody>
</table>
我想迭代 count 以便我的 table 可以有行号。基本上,我初始化了 $count=1
。我怎样才能用干净高效的代码来实现它?还是有更好更简单的方法?
已更新 对于 Mustache,您需要创建一个函数
var data = {
items: allnames,
, index: function() {
return ++window['index']||(window['index']=0);
}
}
然后在循环中使用{{index}}
原本
抱歉,我正在考虑如下所示的车把(请参阅上面的正确答案)
使用{{@index}}
{{#allnames}}
{{name}} is {{@index}}
{{/allnames}}
注意:索引从零开始