有没有办法为我在Meteor集合中拥有的物品的编号列表获取“编号”?我知道我可以在html中做到这一点,但我觉得如果我只需要在{{spacebar}}中放置一些东西,样式就会容易得多。如果我可以使用更好的术语,请让我知道。就像这样。
前20部电影排行榜:
{{#each movie}}
Movie #{{number}} {{movie_name}} {{review_score}}
{{/each}}发布于 2015-03-27 04:55:22
使用Underscore.js中的"map“尝试一下。
我期待你的"Movie“收藏中有电影,它们看起来像这样:
{
title: "Shawshank Redemption",
score: 92
},
{
title: "Forrest Gump",
score: 96
},
{
title: "Green Mile",
score: 91
},
{
title: "The Godfather",
score: 95
}..。以此类推。
下面是"yourTemplate“辅助函数:
Template.yourTemplate.helpers({
movie: function () {
var loadMovies = Movie.find({}, {sort: {score: -1}, limit: 20}).fetch(); // added descending sorting by review score
var array = _.map(loadMovies, function(movie, index) {
return {
number: index+1, // incrementing by 1 so you won't get 0 at the start of the list
movie_name: movie.title,
review_score: movie.score
};
});
return array;
}
});因此,现在您可以在模板中使用它,如下所示:
<template name="yourTemplate">
{{#each movie}}
Movie #{{number}} {{movie_name}} {{review_score}}
{{/each}}
</template>https://stackoverflow.com/questions/28626410
复制相似问题