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

dsc*_*dsc 5 javascript meteor meteor-autoform

我一直在搜索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>
Run Code Online (Sandbox Code Playgroud)

然后这些有助手填充选项:

Template.processFormTemplate.helpers({
  elements: function() {
    return getFormElements();
  },
  categories: function(elementId) {
    return getFormCategories(this.doc.elementId);
  }
});
Run Code Online (Sandbox Code Playgroud)

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;

};
Run Code Online (Sandbox Code Playgroud)

现在我知道这不起作用,因为帮助器不是被动的,但是我不知道如何改变这种行为.我也试过挂钩'变化'事件,但这从来没有因某些原因而引发:

Template.processFormTemplate.events({
 'change #elementId': function(e) {
  console.log($('[name="elementId"]').val() + ' is now selected');
}
});
Run Code Online (Sandbox Code Playgroud)

所需的行为是,当在第一个列表中选择新的elementId时,应根据所选的elementId刷新第二个中的选项列表.

任何帮助非常感谢.

谢谢,大卫

Wop*_*ppi 12

在我花了几个小时才解决之前,我遇到了同样的问题.您必须使用简单模式来获取所选选项的值,使用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
} 
});
Run Code Online (Sandbox Code Playgroud)

顺便说一下,由于在5.0中使用Autoform.getFieldValue时遇到的问题,我仍然使用autoform@4.2.2

5.0.3中的问题我已经报告过:https://github.com/aldeed/meteor-autoform/issues/785#issuecomment-84600515

  • 您好 - 感谢您的答案...您将如何在需要相同行为的多种表格中使用您的解决方案? (2认同)

dsc*_*dsc 5

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

  1. 我需要在第一个选择控件中添加一个'id',这样我才能捕获它的更改事件:

        {{> afQuickField name="elementId" id="elementId" options=elements}}
    
    Run Code Online (Sandbox Code Playgroud)

然后我使用了一个Reactive Dict变量,然后我在更改的事件中设置了该变量.会话变量可能会做同样的工作.

Template.processFormTemplate.events({
 'change #elementId': function() {
  dict.set('activeElementId', $('select#elementId').val());
}
});
Run Code Online (Sandbox Code Playgroud)

并将其用作我的类别助手中的参数:

  categories: function(elementId) {
    return getFormCategories(dict.get('activeElementId'));
  }
Run Code Online (Sandbox Code Playgroud)

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