为Meteor数据创建编号列表 [英] Creating a numbered list for Meteor data

查看:61
本文介绍了为Meteor数据创建编号列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法获得我在Meteor集合中的项目编号列表的数字。我知道我可以在html中完成它,但我觉得如果我可以在{{spacebars}}中放弃一些内容,那么风格会更容易。如果我可以使用更好的术语,请告诉我。这样的事情。

Is there a way to get "the number" for a numbered list of the items I have in a Meteor collection. I know I can do it in html, but I feel like it would be much easier to style if I could just drop something in the {{spacebars}}. If there's better terminology I could be using, please let me know. Something like this.

前20部电影列表:

    {{#each movie}}
         Movie #{{number}} {{movie_name}} {{review_score}}
    {{/each}}


推荐答案

尝试使用 Underscore.js

我希望你的电影系列中有电影,它们看起来像这样: / p>

I'm expecting you have movies in your "Movie" collection and they look something like this:

{
    title: "Shawshank Redemption",
    score: 92
},
{
    title: "Forrest Gump",
    score: 96
},
{
    title: "Green Mile",
    score: 91
},
{
    title: "The Godfather",
    score: 95
}

...依此类推......

... and so on ...

这里是yourTemplate辅助函数:

And here is "yourTemplate" helper function:

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;
    }
});

现在您可以在模板中使用它,如下所示:

So now you can use it in your template like this:

<template name="yourTemplate">
    {{#each movie}}
         Movie #{{number}} {{movie_name}} {{review_score}}
    {{/each}}
</template>

这篇关于为Meteor数据创建编号列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆