1

How can you get an item's position in a list?

I'm trying to do something like the following:

Template:

{{#each people}}
  {{position}}.- {{name}}
{{/each}

JS:

Template.leaderboard.people = -> Players.find({}, { sort: { rank: 1 } })
Template.leaderboard.position = -> ???

So that if the data on Players is:

[ 
  { name: "Tom", rank: 1.2 }, 
  { name: "Dick", rank: 0.7 },
  { name: "Harry", rank: 1.5 }
]

The results will be:

1.- Dick
2.- Tom
3.- Harry

Maybe there's a way to do it with a mongo projection but I can't find how.

Update/Answer:

Template.leaderboard.people = -> 
  Players.find({}, { sort: { rank: 1 } }).map (doc,index) ->
    doc.position = index + 1
    doc
Manuel
  • 10,869
  • 14
  • 55
  • 86

1 Answers1

0

This is simpler, and leverages the HTML tag for ordered lists:

<ol>
   {{#each people}}
      <li>- {{name}}</li>
   {{/each}
</ol>

Template.leaderboard.people = -> 
    Players.find({}, { sort: { rank: 1 } })

More details on http://www.w3schools.com/tags/tag_ol.asp

And

http://www.w3schools.com/html/tryit.asp?filename=tryhtml_lists

Giant Elk
  • 5,375
  • 9
  • 43
  • 56