[Vuejs]-How to sum total in table vuejs?

0👍

Not sure what type of data is in ambilPrice or subtotalRow but you can just add another method and pass parameters to do calculations.

<tr v-for="(data, index) in result_estimate.item_tabel" :key="index">
  <td>
    <pre> sub total : {{ambilaja(formatPrice(ambilPrice[index] - (ambilPrice[index] * subtotalRow[index])))}}</pre>
  </td>
<tr>
<div> total : {{calTotals(ambilPrice, ambilPrice, subtotalRow)}}</div>

Vue Code:

return {
  totals : 0
  },
  method : {
    calTotals(ambilPriceArr, subtotalRow, index) {
      this.totals = this.totals + ambilPriceArr[index] - (ambilPriceArr[index] * subtotalRow[index]);
      return this.totals;
    },
}

0👍

The best approach for this issue is to use computed property for the total as like below.

computed() {
    total: function(){
        let sum = 0;
        this.result_estimate.item_tabel.forEach(item => sum += this.calculatePriceForIndividualRow(item));
        return sum;
    },
},

Where calculatePriceForIndividualRow is the function that will calculate sum for each row.

Leave a comment