ramprakash jagadeesan
ramprakash jagadeesan

Reputation: 284

Sum of all loop values | Vuejs | Javascript | Element table

Looping the list of values in element.io table. I need to display the sum of list of weights. I am unable to sum all the weight values.

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 
  },  ]

The above data is my json data. Need to sum of all (Key name: 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>

Received value is

30106 3498 500

Expected value is

34104

Upvotes: 0

Views: 790

Answers (2)

Tech Key
Tech Key

Reputation: 101

Try with this. Hope it will work

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

Add this function in your script.

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

Upvotes: 1

Ali Khan
Ali Khan

Reputation: 110

I think you need to calculate this value in your javaScript and then display the sum.

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

Upvotes: 1

Related Questions