6

根据另一个字段动态显示子模式(对象)的字段的最佳方法是什么?在以下示例中,文档 (Schemas.Main) 可以包含在 Schemas.Items 中定义的多个项目。填写项目所需的字段取决于所选类型。

例如,如果用户选择 type=="type1",则需要填写字段“type1_field1”和“type1_field2”。

一个解决方案可能需要使用 autoForm 并结合 AutoForm.getFieldValue 和 afArrayField 的设置字段,对吗?我尝试了很多组合,但要么无法添加额外的项目(缺少加号),要么我无法添加不同的项目(即所有项目都是 type1)。任何提示如何解决这个问题?

//Schemas.js
Schemas = {}; Collections = {};
Main = Collections.Main = new Mongo.Collection("Main");
Main.attachSchema(Schemas.Main);
Meteor.isClient && Template.registerHelper("Schemas", Schemas);

Schemas.Main = new SimpleSchema({
  name: {
    type: String
  },
  items: {
    type: [Schemas.Items]
  }
});

Schemas.Items = new SimpleSchema({
      type: { //FormActions...
          type: String,
          allowedValues: ['type1', 'type2', 'type3'],
          autoform: {
              type: "selectize"
          }
      },
      type1_field1: {
        type: String
      },
      type1_field2: {
        type: String
      },
      type2_field1: {
        type: String
      },
      type2_field2: {
        type: String
      },
      type3_field1: {
        type: String
      },
      type3_field2: {
        type: String
      }            
});

//form.html
{{#autoForm id="testForm" type="insert" collection=Collections.Main}}
     {{> afFieldInput name='name'}}

     {{> afArrayField name='items' fields="items.$.type, items.$.type1_field1"}} //How to set these fields dynamically depending on type?

    <div class="form-group">
      <button type="submit" class="btn btn-primary">Create</button>
    </div>
{{/autoForm}}  
4

2 回答 2

4

我最后使用了另一种方法,并基于 afArrayField 创建了一个自己的模板,它使用

{{> UI.dynamic template=currentFieldValue}}

不确定这是否是最好的方法,但似乎适用于我的情况:

Template.registerHelper("currentFieldValue", function() {
  return AutoForm.getFieldValue("testForm", this.current.type) || "no type so far";
});



{{#autoForm id="testForm" type="insert" collection=Collections.Main}} 
  {{> afFieldInput name='name'}} 
  {{> afArrayField name='items' id="something" template="mycustom"}}
  <div class="form-group">
    <button type="submit" class="btn btn-primary">Create</button>
  </div>
{{/autoForm}}

<template name="afArrayField_mycustom">
  <div class="panel panel-default">
    <div class="panel-heading">{{afFieldLabelText name=this.atts.name}}</div>
    {{#if afFieldIsInvalid name=this.atts.name}}
    <div class="panel-body has-error">
      <span class="help-block">{{{afFieldMessage name=this.atts.name}}}</span>
    </div>
    {{/if}}
    <ul class="list-group">
      {{#afEachArrayItem name=this.atts.name minCount=this.atts.minCount maxCount=this.atts.maxCount}}
      <li class="list-group-item autoform-array-item">
        <div>
          <div class="autoform-remove-item-wrap">
            {{#if afArrayFieldHasMoreThanMinimum name=../atts.name minCount=../atts.minCount maxCount=../atts.maxCount}}
            <button type="button" class="btn btn-primary autoform-remove-item"><span class="glyphicon glyphicon-minus"></span>
            </button>
            {{/if}}
          </div>
          <div class="autoform-array-item-body">
            <!--all actions have a type -->
            {{> afFieldInput name=this.current.type label=false options="auto"}}
            <!--branch here for other fields that depend on type -->
            {{> UI.dynamic template=currentFieldValue}}
          </div>
        </div>
      </li>
      {{/afEachArrayItem}} 
      {{#if afArrayFieldHasLessThanMaximum name=this.atts.name minCount=this.atts.minCount maxCount=this.atts.maxCount}}
      <li class="list-group-item">
        <button type="button" class="btn btn-primary autoform-add-item" data-autoform-field="{{this.atts.name}}" data-autoform-minCount="{{this.atts.minCount}}" data-autoform-maxCount="{{this.atts.maxCount}}"><span class="glyphicon glyphicon-plus"></span>
        </button>
      </li>
      {{/if}}
    </ul>
  </div>
</template>

<template name="type1">
  <!--include type1 fields here-->
</template>

<template name="type2">
  <!--include type2 fields here-->
</template>

<template name="type3">
  <!--include type3 fields here-->
</template>
于 2015-03-12T10:37:26.360 回答
0

根据 Miriam 的回答,我还想分享我为使事情顺利进行所做的工作。也许它可以受益。

模式 -> 动作

Schemas.actions = new SimpleSchema({
  actions                : {
    type    : Array,
    optional: false,
    minCount: 1,
    autoform: {
      name: "actions"
    }
  },
  "actions.$"            : {
    type: Object
  },
  "actions.$.action_type": {
    type    : String,
    optional: false,
    label   : "Action Type",
    autoform: {
      type   : "select",
      class  : "action_type form-control",
      name   : "action_type",
      label  : "Select type of action",
      options: function()
      {
        let returnValue = [
          {label: "Action 1", value: "action_1"},
          {label: "Action 2", value: "action_2"},
        ];

        return returnValue;
      }
    }
  },
  "actions.$.action_1" : {
    type    : Schemas.action1,
    minCount: 1,
    optional: true,
    label   : "Action 1",
  }
});

模式-> action1

Schemas.action1 = new SimpleSchema({
  "action1_to"     : {
    type    : String,
    optional: false,
    label   : "Email To",
    autoform: {
      type       : "text",
      label      : "Email To",
      placeholder: "Comma seperated values...",
      class      : "form-control"
    }
  },
  "action1_cc"     : {
    type    : String,
    optional: true,
    label   : "Email Cc",
    autoform: {
      type       : "text",
      label      : "Email Cc",
      placeholder: "Comma seperated values...",
      class      : "form-control"
    }
  },
  "action1_subject": {
    type    : String,
    optional: false,
    label   : "Subject",
    autoform: {
      type       : "text",
      label      : "Subject",
      placeholder: "Subject for the Email",
      class      : "form-control"
    }
  },
  "action1_body"   : {
    type    : String,
    optional: false,
    label   : "Email Content",
    autoform: {
      label      : "Email Content",
      rows       : 6,
      class      : "form-control auto-size",
      placeholder: "Email Content goes here...",
      style      : "font-size: 120%;"
    }
  }
});

请注意 Schemas.action1 应该在 Schemas.actions 之前加载,否则您只会呈现一个文本框而不是表单

模板

<template name="actions">
    {{#autoForm id="actions-form" doc=step.data schema=schema}}
        {{> afArrayField name="actions" template="actions" step=step}}
        {{> wizardButtons}}
    {{/autoForm}}
</template>

<template name="afArrayField_actions">
    <div class="panel panel-default">
        <div class="panel-heading">{{afFieldLabelText name=this.atts.name}}</div>
        {{#if afFieldIsInvalid name=this.atts.name}}
            <div class="panel-body has-error">
                <span class="help-block">{{{afFieldMessage name=this.atts.name}}}</span>
            </div>
        {{/if}}
        <ul class="list-group">
            {{#afEachArrayItem name=this.atts.name minCount=this.atts.minCount maxCount=this.atts.maxCount}}
                <li class="list-group-item autoform-array-item">
                    <div>
                        <div class="autoform-remove-item-wrap">
                            {{#if afArrayFieldHasMoreThanMinimum name=../atts.name minCount=../atts.minCount maxCount=../atts.maxCount}}
                                <button type="button" class="btn btn-primary autoform-remove-item"><span
                                        class="glyphicon glyphicon-minus"></span></button>
                            {{/if}}
                        </div>
                        <div class="autoform-array-item-body">
                            {{> afQuickField name=this.current.action_type label=false options=actionOptions}}

                            {{> UI.dynamic template=currentFieldValue data=this }}

                        </div>
                    </div>
                </li>
            {{/afEachArrayItem}}
            {{#if afArrayFieldHasLessThanMaximum name=this.atts.name minCount=this.atts.minCount maxCount=this.atts.maxCount}}
                <li class="list-group-item">
                    <button type="button" class="btn btn-primary autoform-add-item"
                            data-autoform-field="{{this.atts.name}}" data-autoform-minCount="{{this.atts.minCount}}"
                            data-autoform-maxCount="{{this.atts.maxCount}}"><span
                            class="glyphicon glyphicon-plus"></span></button>
                </li>
            {{/if}}
        </ul>
    </div>
</template>

<template name="action_1">
    {{> afQuickField name=this.current.action_1 }}
</template>

您会{{> wizardButtons}}在模板中看到这是因为我使用的是form-wizard

和一个用于currentFieldValue帮助器的Template.registerHelper

Template.registerHelper("currentFieldValue", function()
{
  let val = AutoForm.getFieldValue(this.current.action_type, "actions-form");
  return val || null;
});

希望这对某人有所帮助并节省时间。

感谢 Mariam 提供此解决方案

于 2016-03-01T04:38:03.520 回答