Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
134
rated 0 times [  138] [ 4]  / answers: 1 / hits: 6804  / 10 Years ago, mon, march 3, 2014, 12:00:00

I have a list of objects in meteorjs which I am iterating in meteorjs templates like



{{#each objects}}
{{/each}}


In the template I want to print the number of the loop iteration. That is, if the length of the objects list is 100 I want to print the numbers from 1 to 100 in the template. How can I do this?


More From » meteor

 Answers
3

You can't do this at the moment without giving in an index in your helper, i.e



Template.yourtemplatename.object_with_index = function() {
var objects = Template.yourtemplatename.objects();

for(var i = 0; i=objects.length; i++) {
objects[i].index = i;
}

return objects;
}


Then do:



{{#each object_with_index}}
<p>This is number {{index}}</p>
{{/each}}


Not the prettiest way, but other variations would basically do the same thing under the hood (e.g if you used a map)


[#47226] Sunday, March 2, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
myakylas

Total Points: 66
Total Questions: 85
Total Answers: 95

Location: Guadeloupe
Member since Sat, Aug 22, 2020
4 Years ago
myakylas questions
Thu, Apr 28, 22, 00:00, 2 Years ago
Thu, Apr 8, 21, 00:00, 3 Years ago
Sat, Sep 19, 20, 00:00, 4 Years ago
;