我一直在拖网,所以我想问一些应该非常简单的事情的答案,但对于我的生活,我无法弄清楚。
基本上,我有一个流星自动格式和两个选择控件:
<template name="processFormTemplate">
{{#autoForm id="processForm" collection="Processes" type=formAction doc=doc validation="blur"}}
<div class="col-md-12">
{{> afQuickField name="elementId" options=elements}}
{{> afQuickField name="categoryId" options=categories}}
{{> afQuickField name="title"}}
{{> afQuickField name="desc" rows=4}}
</div>
{{>formButtons}}
{{/autoForm}}
</template>然后,这些机构有帮手来填充以下选项:
Template.processFormTemplate.helpers({
elements: function() {
return getFormElements();
},
categories: function(elementId) {
return getFormCategories(this.doc.elementId);
}
});lib/方法.lib
getFormElements = function() {
var options = [];
Elements.find({}, {sort: {ref:1}}).forEach(function (element) {
options.push({
label: element.title, value: element._id
});
});
return options;
};
getFormCategories = function(elementId) {
var options = [];
var filter = {};
if (!isBlank(elementId)) {
filter.elementId = elementId;
}
Categories.find(filter, {sort: {ref:1}}).forEach(function (d) {
options.push({
label: d.title, value: d._id
});
});
return options;
};现在我知道这是无效的,因为帮助者不是被动的,但是我不知道如何改变这种行为。我也尝试过参与“变化”事件,但这从来没有因为某种原因而引发:
Template.processFormTemplate.events({
'change #elementId': function(e) {
console.log($('[name="elementId"]').val() + ' is now selected');
}
});所需的行为是,当在第一个列表中选择新的elementId时,应该根据所选的elementId刷新第二个选项的列表。
任何帮助都很感激。
谢谢你,大卫
发布于 2015-03-23 03:43:36
在我花了几个小时解决之前,我也有过同样的问题。您必须使用简单的模式来获得所选选项的值,如使用autoform的api调用Autoform.getFieldValue:
Schemas.Store = new SimpleSchema({
center: {
type: String,
optional: true,
autoform: {
type: "select",
options: function () {
return Centers.find().map(function (c) {
return {label: c.name, value: c._id};
});
}
}
},
region: {
type: String,
optional: true,
autoform: {
type: "select",
options: function () {
if (Meteor.isClient) {
var docId = '';
docId = AutoForm.getFieldValue('storesForm', 'center');
return Regions.find({center: docId}).map(function (c) {
return {label: c.name + ' (' + c.code + ')', value: c._id};
});
}
}
}
},
store_name: {
type: String
}
});顺便说一下,由于在5.0中使用Autoform.getFieldValue时遇到的问题,我仍然在使用autoform@4.2.2
在5.0.3中,我已经向aldeed报告了:https://github.com/aldeed/meteor-autoform/issues/785#issuecomment-84600515
发布于 2015-02-23 23:24:35
我成功地做到了-有几件事是错的:
然后,我使用了一个反应性的Dict变量,然后在更改事件中设置该变量。会话变量可能会执行相同的工作。
Template.processFormTemplate.events({
'change #elementId': function() {
dict.set('activeElementId', $('select#elementId').val());
}
});并将其用作类别助手中的参数:
categories: function(elementId) {
return getFormCategories(dict.get('activeElementId'));
}希望这能帮助其他有类似问题的人。
https://stackoverflow.com/questions/28685163
复制相似问题