繁体   English   中英

Meteor-AutoForm:如何根据另一个控件更新选择选项

[英]Meteor-AutoForm: How to update select options based on another control

我一直在搜索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刷新第二个中的选项列表。

任何帮助非常感谢。

谢谢,大卫

在我花了几个小时才解决之前,我遇到了同样的问题。 您必须使用简单模式来获取所选选项的值,使用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中的问题我已经报告过: https//github.com/aldeed/meteor-autoform/issues/785#issuecomment-84600515

我设法让这个工作 - 几件事都错了:

  1. 我需要在第一个选择控件中添加一个'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'));
  }

希望这可以帮助其他人遇到类似的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM