【问题标题】:Creating an array form where each element is a 'select' type创建一个数组形式,其中每个元素都是“选择”类型
【发布时间】:2017-01-17 02:37:26
【问题描述】:

我在我的流星项目中使用 autoform,并且在我的NewStack 表单中为我的dimensions 字段使用afArrayField

目前看起来像这样。

这是它的渲染方式:

NewStack.html

<template name="NewStack">
    <div class="new-stack-container">
        {{#autoForm collection=stacks id="insertStackForm" type="method" meteormethod="createStack" class="new-stack-form"}}
        <fieldset>
            <legend>Add a Stack!</legend>
            {{> afQuickField name='desc'}} 
            {{> afArrayField name='dimensions'}}
        </fieldset>
        <button type="submit" class="btn btn-primary">Insert</button>
        {{/autoForm}}
    </div>
</template>

我希望为每个维度字段看到一个下拉列表,其中填充了我在架构中设置的选项(即dim1dim2dim3)。但是,我似乎无法让表单呈现为纯文本输入以外的任何内容。

Stacks.js

StackSchema = new SimpleSchema({
    desc: {
        type: String,
        label: "Description"
    },
    dimensions: {
        type: [String],
        autoform: {
            type: "select",
            afFieldInput: {
                options: [
                    {label: "dim1", value: 1},
                    {label: "dim2", value: 2},
                    {label: "dim3", value: 3}
                ]
            },

        }
    }
});

有趣的是,如果我在 NewStack.html 中将 afArrayField 更改为 afQuickField,看来 autoform 现在可以看到我的选项(但我显然失去了数组功能)

有什么想法吗? afArrayField 有什么固有的东西使我无法使用某种选择模式吗?

【问题讨论】:

    标签: meteor meteor-blaze meteor-autoform


    【解决方案1】:

    您可以使用$ 为数组中的每个元素指定选项:

    const StackSchema = new SimpleSchema({
      desc: {
        type: String,
        label: "Description"
      },
      dimensions: {
        type: [String],
      },
      "dimensions.$": { // note this entry
        type: String,
        autoform: {
          afFieldInput: {
            options: [
              {label: "dim1", value: 1},
              {label: "dim2", value: 2},
              {label: "dim3", value: 3}
            ]
          },
        }
      }
    });
    

    自动表单中提到了docs

    【讨论】:

    • 完美。非常感谢!我浏览了文档,但并没有完全理解那部分。我真的是 JS 的新手(以前只有 D3 中的玩具可视化)并且不熟悉 $ 语法。我必须等待 19 小时才能获得赏金……但这是你的。
    【解决方案2】:

    尝试将架构更改为:

    dimensions: {
        type: [String],
        autoform: {
            type: "select",
            options: [
                {label: "dim1", value: 1},
                {label: "dim2", value: 2},
                {label: "dim3", value: 3}
            ],
        }
    

    【讨论】:

    • 似乎没有任何改变。
    猜你喜欢
    • 2017-07-07
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    相关资源
    最近更新 更多