![](/img/trans.png)
[英]How to populate autoForm select with values from collection in Meteor?
[英]How to populate Meteor autoform options with objects
我为收集定义了一个简单的架构:
testReport = new SimpleSchema({
name : {
type: String,
label: "Your Name"
},
school : {
type: Object,
label: "Your School"
},
"school.$.name" : {
type: String
},
"school.$.region" : {
type: String
}
}, { tracker: Tracker });
Reports.attachSchema(testReport);
通过自动窗体使用此架构来生成非常简单的窗体,该窗体保存到Reports集合中:
{{#autoForm collection=Collections.Reports id="form" type="insert"}}
{{> afQuickField name="name" }}
{{> afQuickField name="school" options=schoolOptions}}
<input type="submit" value="Submit">
{{/autoForm}}
一个简单的帮助器用于使用集合中的数据自动填充选项下拉列表:
schoolOptions: function () {
if (Meteor.userId()) {
Meteor.user().profile.schools)
return Meteor.user().profile.schools.map(function (c) {
return {label: c.name, value: JSON.stringify({ name: c.name, region: c.region})};
});
}
}
一切都成功渲染,但是当我单击“提交”时,在“学校”字段上看到一条错误消息,提示
“您的学校必须是对象类型”
我确实尝试了几次重构,但是没有任何效果。 通常,在选项下拉列表的“值”字段中,我们仅放置字符串,数字等。但是我想在此处传递一个对象(由助手返回),该对象看起来像。
{"name":"someSchoolName","region":"someRegion"}
因此,插入到Collection中的最终文档应如下所示:
{
"name": "John",
"school": {"name":"someSchoolName","region":"someRegion"}
}
谁能帮忙吗? 提前谢谢了!
在您的模式中,更改此
"school.$.name" : { type: String},"school.$.region" : {type: String}
改为"school.name" : {type:String},"school.region" : {type: String}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.