【问题标题】:Vue 2 - Calculate total of rows inputVue 2 - 计算输入的总行数
【发布时间】:2017-07-31 21:30:49
【问题描述】:

我有一个带有数量和价格输入的动态表,我使用计算属性来计算每一行的总数。 现在我需要找到一种方法来计算总计(所有小计的总和)。

HTML:

   <tr v-for="(item, index) in items">
      <td><input v-model.number="item.qty" size="10"></td>
      <td><input v-model.number="item.price" size="10"></td>
      <td><input v-model.number="subtotalRow[index]" readonly size="10"></td>
      <td><button @click="addRow(index)">+</button></td>
      <td><button @click="removeRow(index)">-</button></td>
   </tr>
   <tr>
      <td>Total: {{total}}</td>
   </tr>

Javascript:

computed: {
    subtotalRow() {
      return this.items.map((item) => {
        return Number(item.qty * item.price)
      });
    },
    // the index part is confusing me
    //
    // total() {
    //  return this.items.reduce((total, ?) => {
    //    return total + ?;
    //  }, 0);
    //}
},

我提供了一个小小提琴来说明问题。

https://jsfiddle.net/h5swdfv5/

我希望一些指导可以帮助我。 提前谢谢你

【问题讨论】:

    标签: vuejs2


    【解决方案1】:
    total() {
      return this.items.reduce((total, item) => {
        return total + item.qty * item.price;
      }, 0);
    }
    

    工作小提琴:https://jsfiddle.net/h5swdfv5/1/

    【讨论】:

    • 谢谢,但是如果我想添加数字格式,例如货币格式呢?
    • @Jazuly 这个问题似乎与这个问题无关,所以请将它作为一个新问题发布。
    猜你喜欢
    • 2017-08-29
    • 1970-01-01
    • 2022-07-31
    • 1970-01-01
    • 2023-02-14
    • 2022-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多