2016-11-14 24 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