繁体   English   中英

所有输入值的总和

[英]Total sum of all input values

我想将每个现有的和附加的价值加起来,总和,我总能看到。

我有点挣扎于如何包含我现有的价值观以及我以后想要添加的价值观。

IncomeList我有两个输入。 一个用于收入newIncomeValue incomeReason 我的List中的每个Item都包含一个 id。 使用sumValue我想显示总金额,但我不知道该怎么做

它看起来像这样:

收入项目.vue

<template>
  <div class="income-item">
      <div class="income-item-left">
          <div v-if="!editing" @dblclick="editincome" class="income-item-label"
            :class="{ completed : completed }">{{ title }}</div>
      </div>
      <div class="income-item-right"> {{ value }} </div>
      <div class="remove-item" @click="removeincome(income.id)">
          &times;
      </div>
  </div>
</template>
<script>
  export default {
      name: 'income-item',
      props: {
          income: {
              type: Object,
              required: true,
          },
          checkAll: {
              type: Boolean,
              required: true,
          }
      },
      data() {
          return {
              'id': this.income.id,
              'title': this.income.title,
              'value': this.income.value,
              'completed': this.income.completed,
              'editing': this.income.editing,
              'beforeEditCache': '',
          }
      },
      watch: {
          checkAll() {
              // if (this.checkAll) {
              //   this.completed = true
              // } else {
              //   this.completed = this.income.completed
              // }
              this.completed = this.checkAll ? true : this.income.completed
          }
      },
      directives: {
          focus: {
              inserted: function (el) {
                  el.focus()
              }
          }
      },
      methods: {
          removeincome(id) {
              this.$emit('removedincome', id)
          },
      }
  }
</script>

收入列表.vue

<template>
  <div>
      <input type="text" class="income-input" placeholder="What needs to be done" v-model="newIncome" @keyup.enter="addincome">
      <input type="text" class="value-input" placeholder="€" v-model="newIncomeValue" @keyup.enter="addValue">
      <transition-group name="fade" enter-active-class="animated fadeInUp" leave-active-class="animated fadeOutDown">
        <income-item v-for="income in incomesFiltered" :key="income.id" :income="income" :checkAll="!anyRemaining"
                    @removedIncome="removeincome" @finishedEdit="finishedEdit">
        </income-item>
      </transition-group>

      <div class="extra-container">
          <div><label><input type="checkbox" :checked="!anyRemaining" @change="checkAllincomes"> Check All</label></div>
          <div>{{ remaining }} items left</div>
      </div>
      <div class="sum-container">
        <div><label> Einkommen: </label></div>
        <div>{{ sumValue }} €</div>
      </div>
  </div>
</template>

<script>
  import IncomeItem from './IncomeItem'
  export default {
  name: 'income-list',
  components: {
    IncomeItem,
  },
  data () {
    return {
      newIncome: '',
      newIncomeValue: '',
      idForincome: 3,
      incomes: [
        {
          'id': 1,
          'title': 'Finish Vue Screencast',
          'value': 300,
          'completed': false,
          'editing': false,
        },
        {
          'id': 2,
          'title': 'Take over world',
          'value': 315,
          'completed': false,
          'editing': false,
        },
      ]
    }
  },
  computed: {
    remaining() {
      return this.incomes.filter(income => !income.completed).length
    },
    anyRemaining() {
      return this.remaining != 0
    },
    incomesFiltered() {
      return this.incomes
    },
    sumValue() {
      var total = parseInt(document.getElementsByClassName('newIncomeValue').value)
      return total;
    },
  },
  methods: {
    addincome() {
      if (this.newIncome.trim().length == 0) {
        return
      }
      this.incomes.push({
        id: this.idForincome,
        title: this.newIncome,
        value: this.newIncomeValue,
        completed: false,
      })
      this.newIncome = ''
      this.newIncomeValue = ''
      this.this.idForincome++
    },
    removeincome(id) {
      const index = this.incomes.findIndex((item) => item.id == id)
      this.incomes.splice(index, 1)
    },
    checkAllincomes() {
      this.incomes.forEach((income) => income.completed = event.target.checked)
    },
    clearCompleted() {
        this.incomes = this.incomes.filter(income => !income.completed)
    },
    finishedEdit(data) {
      const index = this.incomes.findIndex((item) => item.id == data.id)
      this.incomes.splice(index, 1, data)
    },
    //Same for Value
    addValue() {
      if (this.newIncomeValue.trim().length == 0) {
        return
      }
      this.incomes.push({
        id: this.idForincome,
        title: this.newIncome,
        value: this.newIncomeValue,
        completed: false,
      })
      this.newIncome = ''
      this.newIncomeValue = ''
      this.this.idForincome++
      },
    }
  }
</script>

如果你想对你的incomesFilteredvalue属性求和,你可以在你的计算中使用reduce

sumValue() {
  return this.incomesFiltered.reduce((a, c) => a + c.value, 0);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM