D
D
Dauren S2018-12-27 07:26:55
Vue.js
Dauren S, 2018-12-27 07:26:55

Track input values ​​in Vue.js input?

<div id="app">
  <h1>Add user</h1>
  <div v-for="(user, index) in users">
    <input v-model="user.name">
    <input v-model="user.procent">
    <button @click="deleteUser(index)">
      delete
    </button>
  </div>
  
  <button @click="addUser">
    New User
  </button>
  
  <pre>{{ $data }}</pre>
</div>

new Vue({
  el: '#app',
  data: {
    users: [{ name: '',procent:'' }] 
  },
  methods: {
    addUser: function () {
      this.users.push({ name: '',procent:'' });
    },
    deleteUser: function (index) {
      console.log(index);
      console.log(this.finds);
      this.users.splice(index, 1);
      if(index===0)
      this.addUser()
    }
  }
});

Question: How to find out each percentage entered in a line and finally get their sum, take into account also the change in the added lines (percentage)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Khokhlov, 2018-12-27
@dauren101

{
  computed: {
    percentTotal() {
      return this.users.reduce((acc, user) => acc + user.percent, 0)
    },
  },
}

https://ru.vuejs.org/v2/guide/computed.html
https://developer.mozilla.org/ru/docs/Web/JavaScri...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question