D
D
Dauren S2019-01-02 20:37:35
Vue.js
Dauren S, 2019-01-02 20:37:35

Put data from API in options vue.js?

<div id="app">
  <h1>Add user</h1>
  <div v-for="(user, index) in users">
    <select v-model="user.name">
      <option value="">Выбрать</option>
      <option v-for="o in getOptions(index)" :value="o.value" v-text="o.label"></option>
    </select>
    <input v-model="user.procent">

    <button @click="deleteUser(index)">
      delete
    </button>
  </div>

  <button @click="addUser" :disabled="users.length >= options.length">
    New User
  </button>

  <pre>{{ users }}</pre>
  <pre>{{ percentTotal }}</pre>
</div>

new Vue({
  el: '#app',
  data: {
    users: [{ name: '', procent: '' }],
    options: [
      { label: 'Иванов', value: 'ivanov' },
      { label: 'Петров', value: 'petrov' },
      { label: 'Титарев', value: 'titarev' },
    ]
  },
  methods: {
    getOptions(index) {
      return this.options.filter(o => this.users.every((u, i) => u.name !== o.value || i === index));
    },
    addUser() {
      this.users.push({ name: '',procent:'' });
    },
    deleteUser(index) {
      this.users.splice(index, 1);
      if (this.users.length === 0) {
        this.addUser();
      }
    },
  },
  computed: {
    percentTotal() {
      return this.users.reduce((acc, user) => acc + parseInt(user.procent, 10), 0)
    },
 	},
});

How to put data into options from API?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dauren S, 2019-01-02
@ dauren101

In general, I decided something like this

<div id="app">
  <h1>Add user</h1>
  <div v-for="(user, index) in users">
    <select v-model="user.title">
      <option value="">Выбрать</option>
      <option v-for="o in getOptions(index)" :value="o.id" v-text="o.title"></option>
    </select>
     <input v-model="user.percent">

    <button @click="deleteUser(index)">
      delete
    </button>
  </div>

  <button @click="addUser" :disabled="users.length >= options.length">
    New User
  </button>

  <pre>{{ users }}</pre>
  <pre>{{ percentTotal }}</pre>
</div>

new Vue({
  el: '#app',
  data: {
    users: [{ name: '', percent: '' }],
    options: [
      { title: 'Иванов', id: 'ivanov' },
      { title: 'Петров', id: 'petrov' },
      { title: 'Титарев', id: 'titarev' },
    ]
  },
  methods: {
    getOptions(index) {
      return this.options.filter(o => this.users.every((u, i) => u.title !== o.id || i === index));
    },
    addUser() {
      this.users.push({ title: '',percent:'' });
    },
    deleteUser(index) {
      this.users.splice(index, 1);
      if (this.users.length === 0) {
        this.addUser();
      }
    },
  },

   created: function () {
    // Alias the component instance as `vm`, so that we  
    // can access it inside the promise function
    var vm = this
    // Fetch our array of posts from an API
    fetch('https://jsonplaceholder.typicode.com/posts')
      .then(function (response) {
        return response.json()
      })
      .then(function (data) {
        vm.options = data
      })
  },
   computed: {
    percentTotal() {
      return this.users.reduce((acc, user) => acc + parseInt(user.percent, 10), 0)
    },
  },
 
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question