Meteor AutoForm:如何基于其他控件更新选择选项
Meteor-AutoForm: How to update select options based on another control
我一直在搜索SO问题,寻找一些本应非常简单的答案,但就我的一生而言,我无法找到答案。
基本上,我有一个流星自动成型与两个选择控制:
<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进行刷新。
非常感谢您的帮助。
谢谢,David
我之前也遇到过同样的问题,花了几个小时才解决。您必须使用简单的模式来获得所选选项的值,如使用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
}
});
顺便说一句,我还在用autoform@4.2.2由于在5.0 中使用Autoform.getFieldValue时遇到问题
5.0.3中的问题我已向alded报告:https://github.com/aldeed/meteor-autoform/issues/785#issuecomment-84600515
我已经设法让它工作起来了——有几件事是错误的:
-
我需要在第一个选择控件中添加一个"id",这样我就可以捕捉到它的更改事件:
{{> afQuickField name="elementId" id="elementId" options=elements}}
然后,我使用了一个Reactive Dict变量,然后在更改的事件中设置该变量。会话变量可能会起到同样的作用。
Template.processFormTemplate.events({
'change #elementId': function() {
dict.set('activeElementId', $('select#elementId').val());
}
});
并将其用作我的分类助手中的参数:
categories: function(elementId) {
return getFormCategories(dict.get('activeElementId'));
}
希望这能帮助其他有类似问题的人。
相关文章:
- 更新输入类型=文本值或文本框控件调整大小事件
- Meteor AutoForm:如何基于其他控件更新选择选项
- 如何在三个js上更新Trackball控件
- 从自定义用户控件更新contenttemplate下页面中控件的id
- Jquery.拆分数组并更新控件.(包括JSFiddle)
- ngModel.setViewValue 不更新 html 控件
- 如何在我的 aspx 上使用条件更新面板调用自定义控件 javascript
- 如何防止 Javascript 更新整个控件和刷新内容
- 如何从地图控件的控制器中更新标记或添加新标记
- 使用 javascript 更新 asp.net 下拉列表控件
- Jquery ui滑块控件不更新值
- 在javascript更新后,从web用户控件获取codebehind中的asp:TextBox文本属性
- 如何在Javascript中异步更新动态生成的HTML控件
- 使用弹出控件扩展程序Asp.net在更新面板内维护滚动位置复选框列表
- GridView RowCommand未更新控件值
- Asp.net用户控件和更新面板打破javascript
- 如何使用JQuery访问更新面板中的控件
- 无法更新在Firefox浏览器上使用AjaxFileUpload控件的asp.net WebForm中的标签
- 根据多个复选框控件的值更新输入项的值
- 如果以编程方式更改了模型,Ko不会更新绑定控件