问题描述
所以我有一个流星收藏.该集合中的每个对象/文档都可以具有未知的结构.也就是说,直到运行时,我才知道每个属性的名称,也不知道有多少.
So I have a Meteor Collection. Each object/document in that collection can have an unknown structure. That is to say, I don't know the name of each property, nor how many there are, until runtime.
本质上,集合中的每个对象都是由人们通过我的前端页面提供的任意数据创建的(通过CSV上传,效果很好).因此,当Meteor启动时,我不会初始化集合.
Essentially, each object in the collection is created from arbitrary data people provide via my front-end page (via CSV upload, which works fine). So I don't initialize the collection when Meteor starts.
现在,我想在HTML页面中创建一个表来呈现集合,但是无需我预先定义需要多少列及其名称.
Now I'd like to create a table in my HTML page, that renders the collection, but without me pre-defining how many columns are needed and what their names are.
那么如何动态设置Spacebars/HTML模板中的列数和名称?
So how do I set the number and names of the columns in my Spacebars/HTML template dynamically?
所以这是我在模板方面所走的距离:
So here's how far I've got on the template side:
<table>
{{#each rows}}
{{> row}}
{{/each}}
</table>
...以及模板:
<template name="row">
{{#if header}} <!-- header is explicitly set, so this is fine -->
<th>
{{#each WHAT?}}
<td>{{???}}</td>
{{/each}}
</th>
{{else}}
<tr>
{{#each WHAT?}}
<td>{{???}}</td>
{{/each}}
</tr>
{{/if}}
</template>
我尝试在Spacebars和Blaze文档中找到任何参考,但是所有示例始终要求我从一开始就知道列的名称.
I tried finding any reference in Spacebars and Blaze documentation, but all examples always require me to know the names of the columns from the get-go.
有什么想法吗?
这是一个示例对象,我通过header属性将其明确标识为header:
Here's an example object, which I am explicitly identifying as the header via the header property:
...和行"看起来像这样:
...and "rows" look like this:
所以我显然撒谎了,因为我的属性/列名称始终是索引号.
So I lied apparently, in that my property/column names are always index numbers.
要回答另一个问题:确定数据集(填充了集合)后,所有对象都具有相同数量的属性(例如,假设有一个csv表,这就是我的数据将始终来自此表).
To answer another question:Once the data set is determined (the collection populated), all objects have the same number of properties (i.e. imagine a csv table, which is where my data will always come from).
推荐答案
从mongo db数据建模pov中,可以通过将变量字段粘贴到数组中来轻松改进此设计.即
From a mongo db data modeling pov this design can be readily improved by sticking your variable fields into arrays. i.e.
{ items:
[
"wexfr",
"123x",
"ewfxc",
"rgc"
],
_id: "NYz84Qhu901MPab",
header: false,
createdAt: "2015-04-08T19:46:24"
}
然后,您可以使用以下模板:
Then you can have templates such as:
{{#each row}}
{{#each items}}
这篇关于如何遍历“流星空格键"模板中的未知对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!