我是JavaScript和Meteor的新手。我设法建立所有服务器端通信,但无法弄清楚如何在我的集合中呈现数据块。就是这样:
{"NAME1":{"service":"NAME1","count":0,"maxCount":0},"NAME2":{"service":"NAME2","count":1,"maxCount":1},"NAME3":{"service":"NAME3","count":2,"maxCount":2},"NAME4":{"service":"NAME4","count":3,"maxCount":3},"NAME5":{"service":"NAME5","count":4,"maxCount":4},"NAME6":{"service":"NAME6","count":5,"maxCount":5}}
我可以通过调用标记名称来提取此块。在那之后,我被困住了。每个NAME作为键是一个动态字符串,我只知道值是 - service,count和maxCount的数组,我希望这些值以表格形式显示3列 - 名称,计数和最大值。我可能应该使用光标进行此块渲染,但无法弄清楚如何执行此操作。请帮忙。
我要呈现的JSON块位于“p”标记内
<template name="template_name">
<h2> Data </h2>
{{#each all_blocks}}
{{#with block_i_need }}
<p>"block i need: " {{.}}</p>
{{/with}}
{{/each}}
</template>`
如何将block_i_need传递给函数并返回service,count和maxCount值?
答案 0 :(得分:0)
您可以使用_.values
提取对象的值数组。
Template.whatever.services = function() {
var object = getThatJsonBlockSomehow();
// object is {"NAME1":{"service":"NAME1","count":0,"maxCount":0}, ...};
return _.values(object);
// returns [{"service":"NAME1","count":0,"maxCount":0}, ...]
}
然后,在模板中,您可以使用#each
迭代该数组。 (#each
允许您遍历数组或游标。)
<template name="whatever">
<table>
<tr>
<th>Service</th><th>Count</th><th>Max count</th>
</tr>
<!-- iterate over the [{"service":"NAME1","count":0,"maxCount":0}, ...] array -->
{{#each services}}
<tr>
<td>{{service}}</td><td>{{count}}</td><td>{{maxCount}}</td>
</tr>
{{/each}}
</table>
</template>