Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

131
Visualizações
V-model doesn't update on checkbox list change

I have a list of checkboxes that are rendered with a v-for on a component. When checked, the checkboxes fill up an array with currently selected checkboxes. The problem comes when one or more of the checked checkboxes is removed - the v-model still counts the removed checkbox. How do I update the v-model as my array updates? I tried force re-rendering the whole component which solves the problem but it's not the solution I need.

  <div v-for="player in players" :key="player.id">
  <input
    v-model="selectedPlayers"
    :value="player.id"
    type="checkbox"
    :id="player.id"
  />
  <label :for="player.id">
    {{ player.name }}
  </label>
</div>

Sandbox

Problem

enter image description here

Desired outcome

enter image description here

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

V-model won't remove data when the component is no longer rendered, you need to do that explicitly.

You could filter selectedPlayers from the @click handler so that it only includes ids that are in the new variable.

this.selectedPlayers = this.selectedPlayers.filter(
  id => players2.find(
    player => player.id === id
  )
)
about 4 years ago · Juan Pablo Isaza Relatório

0

So from what I understand, you have this player2 array, that you need to compare against. Whatever is there in the players2 array needs to be there in the selectedPlayers array. To do this just use the use map array function to iterate over the players2 array to return only the ids of the players and then store them in the selected players array which being a reactive property will automatically patch the DOM. There's absolutely no need to re-render the component.

Vue.config.productionTip = false;
Vue.config.devtools = false;

new Vue({
  el: "#app",
  data() {
    return {
      selectedPlayers: [],
      players: [
        {name: "Arnold",id: "1"},
        {name: "Rambo",id: 2},
        {name: "Terminator",id: 3},
        {name: "Titan",id: 4},
        {name: "Odin",id: 5},
      ],
      players2: [
        {name: "Titan",id: 4},
        {name: "Odin",id: 5},
      ],
    };
  },
  methods: {
    clicked() {
      this.players = this.players2;
      this.selectedPlayers = this.players2.map(p => p.id);
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  If you check all checkboxes and press the button, the array still contains all elements
  <div v-for="player in players" :key="player.id">
    <input v-model="selectedPlayers" :key="players.length" :value="player.id" type="checkbox" :id="player.id" />
    <label :for="player.id">
        {{ player.name }}
      </label>
  </div>
  {{ selectedPlayers }}
  <button @click="clicked">Press me</button>
</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda