JoethaCoder
JoethaCoder

Reputation: 506

Meteor List item count

I am fairly new to Meteor and MongoDB. I am simply trying to print a task list to a table and would like to grab the order of each task in the DB. If a task is deleted then it would be reordered. I know this may be a two part question.

I have been able to grab the total number of items in the collection using:

Template.task.helpers({
  taskCount() {
    return Tasks.find().count(this._id)
  }
});

I am wondering if I am on the right path or if someone has a suggestion about how this could be accomplished.

Upvotes: 0

Views: 275

Answers (1)

Brian Shamblen
Brian Shamblen

Reputation: 4703

There's a @index helper function built into Blaze that returns the zero based index for the current item:

.html file

<ul>
{{#each tasks}}
    <li>{{plus1 @index}}. {{title}}</li>
{{/each}}
</ul>

.js file

Template.task.helpers({
  plus1(index) {
    return index + 1;
  }
});

Upvotes: 2

Related Questions