所有循环值的总和| Vuejs | Javascript |元素表

时间:2019-09-30 10:42:23

标签: javascript json vue.js vuejs2 element

在element.io表中循环显示值列表。我需要显示权重列表的总和。我无法对所有重量值求和。

var val= [ 
  { 
    "NmVehiclePlanning.deliveryBranch": "Madurai", 
    "NmVehiclePlanning.deliveryCity": "Madurai", 
    "NmVehiclePlanning.weight": 30106 
  }, 
  { 
    "NmVehiclePlanning.deliveryBranch": "Madurai", 
    "NmVehiclePlanning.deliveryCity": "Virudhunagar", 
    "NmVehiclePlanning.weight": 3498 
  }, 
  { 
    "NmVehiclePlanning.deliveryBranch": "Madurai", 
    "NmVehiclePlanning.deliveryCity": "KANYAKUMARI", 
    "NmVehiclePlanning.weight": 500 
  },  ]

以上数据是我的json数据。需要全部加起来(键名:NmVehiclePlanning.weight)

<el-table-column label="weight">
    <template slot-scope="scope">
        <span v-for="data in scope.row.details">
            {{ data['NmVehiclePlanning.weight'] }}
        </span>
     </template>
</el-table-column>

接收值为

30106 3498 500

预期值为

34104

2 个答案:

答案 0 :(得分:1)

我认为您需要在javaScript中计算该值,然后显示总和。

calculateSum(){
    var sum = 0;
    for(let value in val){
        sum += value['NmVehiclePlanning.weight']
    }
    return sum;
}

答案 1 :(得分:1)

尝试一下。希望它能工作

<el-table-column label="weight">
    <template slot-scope="scope">
        {{ calculateSum(scope.row.details) }}
    </template>
</el-table-column>

在脚本中添加此功能。

calculateSum(val){
    var sum = 0;
    for(let value in val){
        sum += val[value]['NmVehiclePlanning.weight']
    }
    return sum;
},