douwuying4709
2015-09-22 03:36
浏览 33
已采纳

我可以迭代胡子渲染的对象吗?

I have table generated by mustache loop that looks like this:

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>

And I want to iterate count so that my table can have row numbers. basically, i initialized $count=1. How can I implement that with clean efficient code? or is there a better simpler way?

图片转代码服务由CSDN问答提供 功能建议

我有胡子循环生成的表,如下所示:

< strong> names.mustache

 &lt; table class =“table table-striped table-hover table-bordered”&gt; 
&lt; thead&gt; 
  &lt; tr&gt; 
&lt; th&gt; 
#
&lt; / th&gt; 
&lt; th&gt; 
名称
&lt; / th&gt; 
&lt; / tr&gt; 
&lt; / thead&gt;  
&lt; tbody&gt; 
 {{#allnames}} 
 {{{active}} 
&lt; tr&gt; 
&lt; td&gt; 
 {{count}} 
&lt; / td&gt; 
  &lt; td&gt; 
 {{name}} 
&lt; / td&gt; 
&lt; / tr&gt; 
 {{/ active}} 
 {{/ allnames}} 
&lt; / tbody&gt; 
&lt  ; / table&gt; 
   
 
 

我想迭代计数,以便我的表可以有行号。 基本上,我初始化 $ count = 1 。 如何使用干净有效的代码实现它? 还是有更简单的方法?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doujiabing1228 2015-09-22 03:44
    已采纳

    UPDATED For Mustache, you need to create a function

    var data = {
        items: allnames,
        , index: function() {
            return ++window['index']||(window['index']=0);
        }
    }
    

    and then use {{index}} inside the loop

    originally

    Sorry, I was thinking of handlebars which works as below (see correct answer above)

    Use {{@index}}

    {{#allnames}}
        {{name}} is {{@index}}
    {{/allnames}}
    

    Note: index starts from zero

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题