Meteor: Custom AutoForm with array of objects

后端 未结 2 1302
猫巷女王i
猫巷女王i 2021-01-04 20:17

I have a SimpleSchema which includes an array of objects:

Things.attachSchema( new SimpleSchema({
    name: {
        type: String,
        label: \"Name\",
         


        
相关标签:
2条回答
  • 2021-01-04 20:30

    You can add buttons to add/remove the array items as so:

    {{#autoForm collection="things" id="myForm" }}
        {{> afQuickField name='schemaName'}}
    
        {{#afEachArrayItem name="fields"}}
    
            <button type="button" class="btn btn-primary autoform-remove-item"><span class="glyphicon glyphicon-minus"></span></button>
            {{> afFieldInput name=this.current.name}}  
            {{> afFieldInput name=this.current.amount}}
    
        {{/afEachArrayItem}}
        <button type="button" class="btn btn-primary autoform-add-item" data-autoform-field="fields"><span class="glyphicon glyphicon-plus"></span></button>
    
    {{/autoForm}}
    

    This will use the built-in buttons and icons for AutoForm, so feel free to modify the HTML as necessary.

    0 讨论(0)
  • 2021-01-04 20:54

    To access the fields of the objects within the array, you can use:

    this.current
    

    So to fix the example given above, use:

    {{#autoForm collection="things" id="myForm" }}
        {{> afQuickField name='schemaName'}}
    
        {{#afEachArrayItem name="fields"}}
    
            {{> afFieldInput name=this.current.name}}  
            {{> afFieldInput name=this.current.amount}}
    
        {{/afEachArrayItem}}
    
    {{/autoForm}}
    

    I don't know if this is the correct way to do this, but it seems to work.

    0 讨论(0)
提交回复
热议问题