Situation 1 - I check my individual checkboxes, my header checkbox gets checked. This is method 1 and works fine.
Code for same
index.vue
<VCheckbox
checked={this.checkSelections[idx]}
nativeOnChange={e => {
this.$set(this.checkSelections, idx, e.target.checked);
let allSelected = true;
for (let i = 0; i < this.checkSelections.length; i++) {
allSelected = this.checkSelections[i];
if (!allSelected) break;
}
this.$root.$emit("checkmarkHead", { allSelected });
}}
/>
Head.vue
mounted() {
this.$nextTick(() => {
this.$root.$on("checkmarkHead", ({ allSelected }) => {
console.log("checkmarkHead", allSelected);
this.allSelected = allSelected;
});
}
},
Situation 2 -
I check my header checkbox and all my checkboxes are checked. Vice-versa is true as well. So method 2 corresponding to this works fine too.
Code for same -
Head.vue
<HeadItem>
<VCheckbox
checked={this.allSelected}
nativeOnChange={e => {
this.allSelected = e.target.checked;
this.$root.$emit("selectAll", {
allSelected: this.allSelected
});
}}
/>
</HeadItem>
index.vue
mounted() {
this.$nextTick(() => {
this.$root.$on("selectAll", ({ allSelected }) => {
this.checkSelections = Array(this.sortedData.length).fill(allSelected);
});
});
}
Problem - When I do situation 2 after Situation 1, the same methods don't work as expected. The view isn't updated. Similarly, executing Situation 1 after Situation 2 won't work either.
Here's the link to
Code Link - https://codesandbox.io/s/vmwy3v4203
I'm clueless now after handling all mutations caveats etc.
I owe you an apology. This is indeed another reactivity issue which could be solved by providing a key attribute ..
You can assign a unique
key
value to an element, which if changed will force Vue to re-render that element. In your case you can assign keys to yourVCheckbox
elements equal to theirchecked
value, forcing Vue to re-render them when they're checked/unchecked. For example ..I've taken the liberty to re-write your
allSelected
property as a computed property and removed the event listener you set up on the root instance. I think it's much cleaner this way ..