D
D
Dauren S2018-12-29 06:43:08
Vue.js
Dauren S, 2018-12-29 06:43:08

Don't show selected values ​​in select list vue.js?

<div id="app">
  <h1>Add user</h1>
  <div v-for="(user, index) in users">
      <select v-model="user.name">
    	<option value="0">Выбрать</option>
    	<option value="ivanov">Иванов</option>
    	<option value="petrov">Петров</option>
    	<option value="titarev">Титарев</option>
    </select>
    <input v-model="user.procent">
  
    <button @click="deleteUser(index)">
      delete
    </button>
  </div>
  
  <button @click="addUser">
    New User
  </button>
  
  <pre>{{ users }}</pre>
  <pre>{{ percentTotal }}</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()
    },
  
  },
   computed: {
      percentTotal() {
      
      return this.users.reduce((acc, user) => acc + parseInt(user.procent, 10), 0)
      },
 	},
});

Question: How can I not show those who have already been selected in the user selection list when adding a new row?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-12-29
@dauren101

Instead of sewing choice options into the template, make an array and pass it to v-for, having previously run it through the filter function, where already selected options will be discarded (except for the current one):

<select v-model="user.name">
  <option value="">Выбрать</option>
  <option v-for="o in getOptions(index)" :value="o.value" v-text="o.label"></option>
</select>

getOptions(index) {
  return this.options.filter(o => this.users.every((u, i) => u.name !== o.value || i === index));
},

https://jsfiddle.net/gs0oenxq/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question