Creating an array form where each element is a 'select' type
Asked Answered
I

2

10

I'm using autoform on my meteor project and am using an afArrayField for my dimensions field in my NewStack form.

It currently looks like this.

autoform image rendering

And here's how it's being rendered:

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>

What I would like to see for each of the dimensions fields is a dropdown populated with the options I set in the schema (i.e. dim1, dim2, and dim3). However I can't seem to get the form to render as anything other than a plain text input.

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}
                ]
            },

        }
    }
});

Interestingly, if I change the afArrayField to afQuickField in NewStack.html It appears that autoform can now see my options (but I lose the array functionality obviously)

select example

Any thoughts? Is there something inherent about afArrayField that precludes me from using some kind of selection mode?

Indonesia answered 9/9, 2016 at 3:4 Comment(0)
W
6

You can specify options for each element in an array using $:

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}
        ]
      },
    }
  }
});

rendered form

It is mentioned in the autoform docs.

Wards answered 11/9, 2016 at 16:55 Comment(1)
Perfect. Thanks so much! I had looked through the docs, but didn't quite get that part. I'm really new at JS (only previous experience had been toy visualizations in D3) and am unfamiliar with the $ syntax. I have to wait 19hrs to award the bounty... but it's yours.Indonesia
S
1

Try changing your schema to:

dimensions: {
    type: [String],
    autoform: {
        type: "select",
        options: [
            {label: "dim1", value: 1},
            {label: "dim2", value: 2},
            {label: "dim3", value: 3}
        ],
    }
Sphygmic answered 9/9, 2016 at 17:16 Comment(1)
Doesn't appear to change anything.Indonesia

© 2022 - 2024 — McMap. All rights reserved.