11

Any idea how to resolve this problem:

in this example, the author uses vue 2.3.2 which works perfect,

new Vue({
  el: '#app',
  data: {
    users: [{
        "id": "Shad",
        "name": "Shad"
      },
      {
        "id": "Duane",
        "name": "Duane"
      },
      {
        "id": "Myah",
        "name": "Myah"
      },
      {
        "id": "Kamron",
        "name": "Kamron"
      },
      {
        "id": "Brendon",
        "name": "Brendon"
      }
    ],
    selected: [],
    allSelected: false,
    userIds: []
  },
  methods: {
    selectAll: function() {
      this.userIds = [];

      if (this.allSelected) {
        for (user in this.users) {
          this.userIds.push(this.users[user].id.toString());
        }
      }
    },
    select: function() {
      this.allSelected = false;
    }
  }
})
<script src="https://cdn.jsdelivr.net/vue/latest/vue.js"></script>

<div id="app">
  <h4>User</h4>
  <div>
    <table>
      <tr>
        <th>Name</th>
        <th>Select All<input type="checkbox" @click="selectAll" v-model="allSelected"></th>
      </tr>
      <tr v-for="user in users">
        <td>{{ user.name }}</td>
        <td><input type="checkbox" v-model="userIds" @click="select" :value="user.id"></td>
      </tr>
    </table>
  </div>

  <span>Selected Ids: {{ userIds }}</span>
</div>

when I switch it to 2.5.16 ( <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script> ) , the behavior is wierd:

When click the selectAll checkbox, only that checkbox checked, but when I toggle it to uncheck, all the checkboses below get checked

enter image description here

enter image description here

enter image description here

8
  • 3
    It seems to be working here; is it behaving differently in your project? Jul 19, 2018 at 19:10
  • 1
    Example you linked work perfectly fine if change vue link to cdn.jsdelivr.net/npm/[email protected]/dist/vue.js . Same as in original.
    – Aldarund
    Jul 19, 2018 at 19:39
  • @Aldarund I updated my question to show screenshot, only vue script tag changed
    – Kuan
    Jul 19, 2018 at 20:14
  • @DanielBeck I updated my question to show screenshot, only vue script tag changed
    – Kuan
    Jul 19, 2018 at 20:14
  • 2
    it seems you should use @change instead of @click
    – Sphinx
    Jul 19, 2018 at 20:29

2 Answers 2

12

For consistent browser functionality, I can recommended to not use click/change on checkboxes. Instead, bind the checkbox to a value (which you've already done), and then use a watcher on the value. This way, the real value of the checkbox will always accurately represent it's state. So you'd have something like this:

<input type="checkbox" v-model="allSelected">


Vue.component({..., {
    data: function() {
             return {
                allSelected: false,
             }
          }
    },
    watch: {
        allSelected: function(val){
            //Use your source of truth to trigger events!
            this.doThingWithRealValue(val); 
        }
    }
});

You're already using your component data value of allSelected as the source of truth, so you should use this source of truth as the real triggering element value, not a click. Whenever the value of allSelected changes, your code will get ran. This solves the problem without the rendering order weirdness.

2
  • How do you handle cases when either user input or something else could update the data field? For example a user click should update the field and propagate a change the backend but a sync from the backend could also update the checkbox to its latest value? Apr 6, 2020 at 18:49
  • Perhaps I misunderstand, but simply set the data attribute you're modeling to whatever you want. It doesn't matter what changes it, the data attribute itself is what is important. That is the source of truth here- the watcher reacts to a change in your attribute- it does not manage it.
    – rob
    Oct 26, 2020 at 15:03
4

As pointed out by rob in the comments and in his answer you cannot rely on @click / @input / @change to have the same behaviour in all browsers in regards to their execution order relative to the actual model change. There is an issue at the VueJS repository with a bit more context: https://github.com/vuejs/vue/issues/6709

The better solution is to watch the model for changes and then react accordingly.

new Vue({
  el: '#app',
  data: {
    users: [{
        "id": "Shad",
        "name": "Shad"
      },
      {
        "id": "Duane",
        "name": "Duane"
      },
      {
        "id": "Myah",
        "name": "Myah"
      },
      {
        "id": "Kamron",
        "name": "Kamron"
      },
      {
        "id": "Brendon",
        "name": "Brendon"
      }
    ],
    selected: [],
    allSelected: false,
    userIds: []
  },
  methods: {
    selectAll: function() {
      this.userIds = [];

      if (this.allSelected) {
        for (user in this.users) {
          this.userIds.push(this.users[user].id.toString());
        }
      }
    },
    select: function() {
      this.allSelected = false;
    }
  },
  watch: {
      allSelected: function () {
          this.selectAll()
      }
  }
})
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>

<div id="app">
  <h4>User</h4>
  <div>
    <table>
      <tr>
        <th>Name</th>
        <th>Select All<input type="checkbox" v-model="allSelected"></th>
      </tr>
      <tr v-for="user in users">
        <td>{{ user.name }}</td>
        <td><input type="checkbox" v-model="userIds" @click="select" :value="user.id"></td>
      </tr>
    </table>
  </div>

  <span>Selected Ids: {{ userIds }}</span>
</div>

9
  • Thanks, but when I change vue version(only that script tag), same code works differently
    – Kuan
    Jul 19, 2018 at 20:27
  • Edited my answer. VueJS seem to have changed the event order, or how the model is updated.
    – puelo
    Jul 19, 2018 at 20:35
  • 1
    Ah! OK, this is browser-specific; Chrome and Firefox work as you described (behavior reversed in 2.5.16); in Safari both versions behave identically. Jul 19, 2018 at 20:43
  • 2
    Found a closed issue in VueJS for that matter. Added it.
    – puelo
    Jul 19, 2018 at 20:50
  • 1
    You are right. Your solution is better. Will also change my answer
    – puelo
    Jul 19, 2018 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.