简体   繁体   English

如何渲染带有流星空格键模板的表和每行的行号?

[英]How Do I Render Table With Meteor Spacebars Template and Row Number For Each Row?

How can I render a table with a meteor spacebars {{#each}} in a template and for each row add row number in first column like below: 如何在模板中呈现带有流星空格键{{#each}}的表格,并为每一行在第一列中添加行号,如下所示:

| | | | title header | 标题标题| content header | 内容标题|

| | 1 | 1 | first title | 第一个标题| first content | 第一内容|

| | 2 | 2 | second title | 第二标题| second content | 第二内容|

| | 3 | 3 | third title | 第三标题| third content | 第三内容|

This is my each code now: 这是我现在的每个代码:

<table class="table table-bordered table-hover">
    <thead>
    <tr>
        <th></th>
        <th>title</th>
        <th>content</th>
    </tr>
    </thead>
    <tbody>
        {{#each posts}}
            <tr>
            <td>
                *** need index for each row in this column ***
            </td>
            <td>
                {{ title }}
            </td>
            <td>
                {{ content }}
            </td>
            </tr>
        {{/each}}
    </tbody>
</table>

Is there a way to get the array index within the {{#each}} or define extra property named index myself and index++ in each iteration? 有没有一种方法可以在{{#each}}获取数组索引,或者在每次迭代中定义名为index self和index++额外属性?

In your helper that returns the cursor of values, instead of just doing a collection.find() do a collection.find().fetch() This will give you you an array of objects instead of a cursor. 在返回值的游标的帮助器中,不只是执行collection.find()而是执行collection.find().fetch()这将为您提供对象数组而不是游标。 Then simply .forEach() through that an add an index key! 然后只需通过.forEach()来添加索引键! Or you could just use collection.map() and add the key directly to each element as you map the cursor into an array. 或者,您可以只使用collection.map()并将键映射到数组中时直接将键添加到每个元素。 Docs 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM