我一直在拖網SO問題的答案,應該是非常簡單的東西,但對於我的生活我無法弄清楚。Meteor-AutoForm:如何更新基於另一個控件的選擇選項
基本上我有流星自動窗體有兩個選擇控件:
<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/methods.js
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進行刷新。
任何幫助非常感謝。
謝謝, 大衛
該解決方案爲我工作的偉大。謝謝! – Aaron 2015-04-04 21:02:32
嗨那裏 - 謝謝你的答案...你會如何使用你的解決方案跨多種形式需要相同的行爲? – dsc 2015-04-08 12:06:53
@Woppi好奇你爲什麼在你的區域autoform選項函數中有if(Meteor.isClient())...? – Wes 2015-07-29 15:41:52