jeudi 8 octobre 2020

Vuejs strange behavior when a list of checkboxes are updated

Well, it's not easy to explain.

I have a list of checkboxes generated from reactive data. When you check one of the checkboxes, one of the entries of the reactive data is deleted.

The list is then updated.

The next checkbox is then placed under the mouse cursor and is "activated" by releasing the mouse button. This behavior is unwanted, can you see a way to avoid this?

Here is the code to illustrate this case:

<div id="app">
  <h2>Todos:</h2>
  <ol>
    <li v-for="todo in todos">
      <label>
        <input type="checkbox"
          v-on:change="toggle">
        
      </label>
    </li>
  </ol>
</div>

Script part:

new Vue({
  el: "#app",
  data: {
    todos: [
      { text: "Learn JavaScript" },
      { text: "Learn Vue" },
      { text: "Play around in JSFiddle" },
      { text: "Build something awesome" }
    ]
  },
  methods: {
    toggle: function(){
        this.todos.splice(1,1)
    }
  }
})

Also a live test: https://jsfiddle.net/m10jyLut/7/

I don't know if my design is correct. I would like to avoid a too hackneyed solution.

Thank you very much for your guess.




Aucun commentaire:

Enregistrer un commentaire