问题描述
以下示例将生成玩家名称列表,其中玩家是来自数据库。
< template name =players>
{{#each topScorers}}
< div> {{name}}< / div>
{{/每个}}
< / template>
然而,我想连续显示其中的四个,并且在打印四个玩家后,我希望将行分成< hr />
,然后继续。例如,
< template name =players>
{{#each topScorers}}
< div style =float:left;> {{name}}< / div>
{{if index%4 == 0}}
{{/ if}
{{/ each}}
< / template>
如何在迭代集合时做类似的事情?
地图光标功能。
下面是一个示例,展示如何在每个集合中使用索引时返回索引:
index.html:
{{#each items}}
index:{{this.index}}
{{/ each}}
< / template>
index.js:
Items = new Meteor.Collection('items');
Template.print_collection_indices.items = function(){
var items = Items.find()。map(function(doc,index,cursor){
var i = _ .extend(doc,{index:index});
返回i;
});
退货项目;
};
The below example will generate a list of names of players, where players is a data set from a MongoDB database.
<template name="players">
{{#each topScorers}}
<div>{{name}}</div>
{{/each}}
</template>
However, I want to display four of them in a row, and after four players is printed, I want to divide the line by <hr />
and then continue. For instance,
<template name="players">
{{#each topScorers}}
<div style="float:left;">{{name}}</div>
{{if index%4==0}}
<hr style="clear:both;" />
{{/if}
{{/each}}
</template>
How can I do something like that while iterating through collections?
Another solution, in line with maintaining the reactivity of the collection, is to use a template helper with the map cursor function.
Here's an example showing how to return the index when using each with a collection:
index.html:
<template name="print_collection_indices">
{{#each items}}
index: {{ this.index }}
{{/each}}
</template>
index.js:
Items = new Meteor.Collection('items');
Template.print_collection_indices.items = function() {
var items = Items.find().map(function(doc, index, cursor) {
var i = _.extend(doc, {index: index});
return i;
});
return items;
};
这篇关于在遍历流星中的集合时,有没有办法获得索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!