写一个项目时,需要实现一个动态增减table的行数,然后将两列的乘积放在第三列做金额计算。然后将table所有金额进行计算放在form表单里,进行总金额展示
起初的实现:新增行时,form的总金额是跟着变的,但是在table里计算以后,总金额是不会跟着变化
table:
<el-table-column label="计划数量" align="center" width="180">
<template slot-scope="scope">
<el-input-number :disabled="isdisabled" v-model="scope.row.count" :step="1" :max="99999" />
</template>
</el-table-column>
<el-table-column label="单价(元)" align="center" width="180">
<template slot-scope="scope">
<el-input-number :disabled="isdisabled" v-model="scope.row.price" :precision="2" :step="1" :max="99999" />
</template>
</el-table-column>
<el-table-column label="金额(元)" align="center" width="180">
<template slot-scope="scope">
{
{
totalAmount(scope.row.price,scope.row.count) }}
</template>
</el-table-column>
<!--计算属性-->
totalAmount: function() {
return function(arg1, arg2) {
var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
try {
m += s1.split('.')[1].length; } catch (e) {
}
try {
m += s2.split('.')[1].length; } catch (e) {
}
return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m);
};
},
form:
<el-form-item label="总金额(元):" label-width="110px" prop="totalAmount">
<el-input :disabled="isdisabled" :clearable="true" v-model="FormtotalAmount" placeholder="请输入" class="filter-item" />
</el-form-item>
<!--计算属性-->
FormtotalAmount: function() {
if (this.planForm.totalAmount > 0) {
return this.planForm.totalAmount;
} else {
let sum = 0;
if (this.formData.formList.length > 0) {
for (let i = 0; i < this.formData.formList.length; i++) {
sum += this.formData.formList[i].amount;
}
} else if (this.SelfBuyData.formList.length > 0) {
for (let i = 0; i < this.SelfBuyData.formList.length; i++) {
sum += this.SelfBuyData.formList[i].amount;
}
}
return sum;
}
}
在这里就不放图了,我每次计算完打印了下table里的数,发现问题是,我数量和单价都变了以后,table里面的单价和数量都是跟着变,但是table里面的金额还是1,想想不应该啊,计算属性应该返回的是乘积。
最后的解决办法:
改写金额那块,计算属性还是要用,但是前面加上scope.row.amount =
<el-table-column label="金额(元)" align="center" width="180">
<template slot-scope="scope">
{
{
scope.row.amount = totalAmount(scope.row.price,scope.row.count) }}
</template>
</el-table-column>
我也不知道为啥,element的坑吧。。。