flimflam57
flimflam57

Reputation: 1334

How to get final iteration number in Meteor

In Meteor we have the '@index' operator to get the index value of the iteration. But I wanted to get the total number of iterations then print that number on the page. So the page at top might read the total number of boys in a group.

For example, I might have something like:

Total = {{#each StudentMale}} {{formatMaleCount @index}} {{/each}}

and a register helper just to add 1 to the number

Template.registerHelper('formatMaleCount', function (count) {
return count + 1;
});

and this would print:

Total = 1234567

I'd like to have:

Total = 7

Coming up short on how to do this. I tried to have the helper put the values in an array, but this wouldn't work since a new array is produced on each iteration.

Upvotes: 1

Views: 51

Answers (1)

Stephen Woods
Stephen Woods

Reputation: 4049

StudentMale is presumably an array or cursor, so in a new helper:

If it's an array:

arrayLength( array ) {
  return array.length;
}

Or if it's a collection:

studentMaleLength() {
  return StudentMales.find().fetch().length;
}

Then just call your helper.

Upvotes: 1

Related Questions