Autoform插入字段总和

时间:2014-08-19 08:12:09

标签: javascript mongodb meteor

是否有可能(以及如何)使用Meteor和AutoForm插入依赖于相同形式的其他字段的数据库Sum字段,以及要隐藏的Sum字段,示例orderSum字段。

orderSum = orderLength + orderLayers

file.js

Order = new Meteor.Collection("order", {
  schema: {
  'orderLayers': {
        type: Number,
        label: "Layers",
        optional: false,
        decimal: true, 
        min: 0
    },
    'orderLength': {
        type: Number,
        label: "Length",
        optional: false,    
        decimal: true, 
        min: 0
    }
}
});

file.html

{{> quickForm collection="Order" id="insertOrderForm" type='insert' buttonContent="Add Order"}}

谢谢。

1 个答案:

答案 0 :(得分:2)

如果您只想显示SimpleSchema中的某些字段,请使用选项fields

<template name="form">
    {{> quickForm fields='orderLayers,orderLength' collection="Order" id="insertOrderForm" type='insert' buttonContent="Add Order"}}
</template>

如果您想对字段orderLayersorderLength进行一些计算,请在新字段中使用autoValue选项orderSum

Order = new Meteor.Collection("order", {
    schema: {
        'orderLayers': {
            type: Number,
            label: "Layers",
            optional: false,
            decimal: true,
            min: 0
        },
        'orderLength': {
            type: Number,
            label: "Length",
            optional: false,
            decimal: true,
            min: 0
        },
        'orderSum': {
            type: Number,
            optional: false,
            decimal: true,
            autoValue:function(){
                var result = this.siblingField("orderLength").value +this.siblingField("orderLayers").value;
                console.log(result);
                return result
            }
        }
    }
});

如果您想快速测试,请克隆: https://github.com/parhelium/meteor-so-autoform-autovalue-fields