【问题标题】:How to make one field Dependent on other field's value in meteor autoform?如何使一个字段依赖于流星自动表单中其他字段的值?
【发布时间】:2018-04-17 17:50:24
【问题描述】:

我是流星新手,我想创建一个表单,其中一个字段的值决定自动表单中另一个字段的值。让我们将设置类型设为“A”、“B”和“C”,因此当我选择“A”时,将加载一个自动表单。我已将此表单设为通用,即它将为所有 A、B 和 C 显示。

{{#each users}}
        {{> afQuickField name='userEmail' value=userEmail readOnly=true }}
        {{> afQuickField name='Setup' value=type readOnly=true}}  
        {{> afQuickField name='Profile' options='allowed' }}
        {{> afQuickField name='Purpose' }}
        {{> afQuickField name='count' options='allowed' }}
        {{> afQuickField name='PackageDirectory' options='allowed'  }}
        {{> afQuickField name="logName" options=LogName }}
 {{/each}}

计数选项应该是:
1. 对于“A”,计数选项应为 9、11、12。
2. 对于“B”,它是 1。
3. 对于“C”,它是 5。
在架构中,我编写了这样的代码

Setup:{
        type: String,
        label:"Setup",
        optional:false,
       defaultValue:type
      },
 count:{
        type: String,
        label:"No. Of count",
         optional: true,
        allowedValues:["9","11","12"],
        autoform:{
          afFieldInput:{
            firstOption:"(Select the count)"
          }
        }
  }

因此,当我选择设置“A”时,我应该获得三个下拉选项,当我单击“B”和“C”时,我应该分别获得默认值 1 和 5。 谁能解决我的问题?

【问题讨论】:

    标签: javascript node.js meteor mongoose-schema meteor-autoform


    【解决方案1】:

    您可以使用getFieldValue 获取特定字段的值,并基于它从模板帮助程序返回一组optiondefaultValue。 找到文档here

    所以按照你的代码:

    form.html:

        ...
    {{#autoForm id="sampleFormID" ... ... }}
        {{> afQuickField name='Setup' value=type readOnly=true}}  
         {{> afQuickField name='count' options=allowedOptionHelper defaultValue=defaultValueHelper }}
        ...
    
    {{/autoForm}}
    

    form.js

    Template.templateName.helpers({
    
        allowedOptionsHelper: function() {
            if (AutoForm.getFieldValue('Setup', 'sampleFormID') === 'A') {
                return [{label:"9", value:"9"},{label:"11",value:"11"},{label:"12", value:"12"}];
                else
                if (AutoForm.getFieldValue('Setup', 'sampleFormID') === 'B') {
                    // return the options for B
                } else if (AutoForm.getFieldValue('Setup', 'sampleFormID') === 'C')) {
                // return the options for C
            }
        },
    
        defaultValueHelper: function() {
            if (AutoForm.getFieldValue('Setup', 'sampleFormID') === 'A') {
                return 11 //or whatever the defaultValue is;
            }
            //likewise for options B and C
        }
    
    });
    

    schema.js

    ...
    ...
     count:{
            type: String,
            label:"No. Of count",
             optional: true,
            autoform:{
              afFieldInput:{
                firstOption:"(Select the count)"
              }
            }
      }
    ...
    ...
    

    【讨论】:

    • 我已经按照你的建议修改了代码,但是当我选择设置为“A”时,我没有得到计数字段的下拉菜单。
    • 您没有看到下拉字段本身吗?还是您没有看到下拉值。如果是后者,可能与返回类型有关。您可以在 return 语句之前放入 console.log 语句,看看它是否真的返回任何东西。另外,我刚刚注意到我对计数模式类型所做的类型不匹配。我已经修好了。
    • 我没有看到值和下拉列表,我已经检查了返回值,据我了解你提到的是正确的。但在这种情况下它不起作用。默认值在计数字段中设置。
    • 您接受了这个答案。所以我认为你想出了如何让它工作?做了什么?
    • 是的,我已经接受了这个答案,但我仍然没有得到它的工作。可以请帮我让它工作吗?我在之前的 cmets 中已经提到过这个问题。我尝试了所有组合的返回值。但是计数“A”的下拉选项没有出现。但是设置了默认值。
    猜你喜欢
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 2022-01-04
    • 2022-08-05
    • 2022-01-14
    • 2022-01-18
    • 1970-01-01
    相关资源
    最近更新 更多