我有三个字段quantity,price和total。
我只是更新quantityand price,所以total应该自动计算。
如何确保total始终正确更新?我想我应该使用收集挂钩。
我有三个字段quantity,price和total。
我只是更新quantityand price,所以total应该自动计算。
如何确保total始终正确更新?我想我应该使用收集挂钩。
如果您使用自动表单和简单模式,只需使用自动值
'price': { type: Number },
'quantity': { type: Number },
'total': {
type: Number,
autoValue: function () {
const price = this.field('price');
const quantity = this.field('quantity');
if (price.isSet && quantity.isSet) {
if (this.isInsert) {
return quantity.value * price.value;
} else {
return { $set: quantity.value * price.value };
}
}
}
}