0

I'm trying to count each row in a table. Each table row is a new collection. The code below counts the total number of collections and displays that. How do I change it to display the row number.

Path: calc.js

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

Path: 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}}
Monasha
  • 711
  • 2
  • 16
  • 27
bp123
  • 3,217
  • 8
  • 35
  • 74
  • 3
    Just use `@index` in your template. Basically a duplicate of http://stackoverflow.com/questions/24225071/how-can-i-get-the-index-of-an-array-in-a-meteor-template-each-loop – Michel Floyd Nov 14 '16 at 23:50

1 Answers1

0

Here's the helpers

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}}

Or if you follow through the answer given by @Michel Floyd then you need this answer too https://stackoverflow.com/a/22103990/3422755 as {{@index}} will give you starting number as 0

Community
  • 1
  • 1
Monasha
  • 711
  • 2
  • 16
  • 27