2016-11-14 90 views
0

我试图计算表中的每一行。每个表格行都是一个新的集合。下面的代码计算收集的总数并显示它们。我如何将其更改为显示行号。使用{{#each}}显示行号

路径:calc.js

SalaryCalculator: function() { 
    return SalaryCalculator.find({}); 
}, 
SalaryCalculatorCount: function() { 
    return SalaryCalculator.find({}).count(); 
} 

路径:calc.html

{{#each SalaryCalculator}} 
    <tr> 
     <th scope="row">{{SalaryCalculatorCount}}</th> 
     <td>{{specialisation}}</td> 
     <td>{{subSpecialisation}}</td> 
     <td>{{positionTitle}}</td> 
     <td>{{yearsOfExperience}}</td> 
     <td>{{salary}}</td> 
    </tr> 
{{/each}} 
+3

只需在模板中使用'@ index'。基本上重复http://stackoverflow.com/questions/24225071/how-can-i-get-the-index-of-an-array-in-a-meteor-template-each-loop –

+0

简单。谢谢。 – bp123

回答

0

这里的助手

SalaryCalculator: function() { 
    var count = 1; 
    var salCalDetails = SalaryCalculator.find({}); 
    salCalDetails.forEach(function(doc){ 
     doc.rowCount = count; 
     count++; 
    }); 
    return salCalDetails; 
}, 

{{#each SalaryCalculator}} 
<tr> 
    <th scope="row">{{rowCount}} </th> 
    <td>{{specialisation}}</td> 
    <td>{{subSpecialisation}}</td> 
    <td>{{positionTitle}}</td> 
    <td>{{yearsOfExperience}}</td> 
    <td>{{salary}}</td> 
</tr> 
{{/each}} 

或者如果你按照@Michel Floyd给出的答案,那么你也需要这个答案https://stackoverflow.com/a/22103990/3422755 as {{@index}}会给你起始号码为0