用户下拉列表选择中的流星变化集合

时间:2016-09-18 09:31:42

标签: meteor meteor-autoform

我正在将Meteor与autoForm / quickForm结合使用。我有收集,我们称之为“体育”这个例子。该集合使用下拉列表(autoForm中的allowedValues)询问用户本周所做的运动。现在,如果用户选择跑步,我想显示'距离','区域'等。如果用户选择篮球,我可能想要显示'拍摄'等等。

我应该怎么做?创建多个集合或SimpleSchema,还是有不同的首选方法?在Google上找不到任何内容,但我确信这不是一个不常见的问题。如果有人有更多信息的链接已经非常感激。

更新

我正在使用'each'循环来循环我之前定义的所有可能的运动。您是否认为为每项不同的运动创建表单项更有意义?如果是这样,我如何确保在架构中正确配置?提前谢谢!

{{#autoForm collection="Sports" type="update" doc=this id="FieldValueIsForm"}}
  {{#each sports}}
    <h3>{{this.name}}</h3>
    {{> afQuickField name="sports.$.sportTrue" noselect=true }}

    {{#if afFieldValueIs name="sports.$.sportTrue" value=true}}
      {{> afQuickField name="sports.$.sportDistance" value=this.frequency}}
    {{/if}}
  {{/each}}

  <div>
    <button type="submit">Submit</button>
  </div>
{{/autoForm}}

更新2:

我的架构可以在这里找到:http://pastebin.com/SbBSbqW2 我简化了一下,但这是主要内容。对于不同的运动,我需要不同的输入字段。

1 个答案:

答案 0 :(得分:1)

听起来您希望将afQuickField选项与条件一起使用。文档讨论了它here。还有一个代码看起来像here的示例;但是,它看起来像这样:

{{#autoForm collection="FieldValueIs" type="insert" id="FieldValueIsForm"}}
  {{> afQuickField name="a" options="allowed" noselect=true}}
  {{#if afFieldValueIs name="a" value="foo"}}
    {{> afQuickField name="b"}}
  {{/if}}
{{/autoForm}}

您只需要确保通过在架构中正确设置它们,将“a”和“b”设置为所需的选择字段。

更新:

我假设您要在SingleSport集合中存储距离,拍摄的照片等。具体如何存储它取决于您,但它可能如下所示:

SingleSport = new SimpleSchema({
  sportType: {
    type: String
  },
  distanceRun: {
    type: Number,
    decimal: true,
    optional: true
  },
  shotsTaken: {
    type: Number,
    optional: true
  },
  sportTrue: {
    type: Boolean,
    label: "Sport completed",
    autoform:{
        type: "boolean-radios",
        trueLabel: "Enabled",
        falseLabel: "Disabled",
        value: false
    }
  }
});

然后,您可以像这样更改表单的条件部分:

{{#if afFieldValueIs name="sportType" value="running"}}
  {{> afQuickField name="distanceRun"}}
{{/if}}
{{#if afFieldValueIs name="sportType" value="basketball"}}
  {{> afQuickField name="shotsTaken"}}
{{/if}}