It's difficult for me to lift out a chunk of code from my application to demonstrate this problem, so I hope merely my describing it it will be clear enough to elicit some useful feedback.
I have a table of products, populated from an array returned by an AJAX call. As part of the call, I add another property to each object in the array:
_.each(this.productList, (product, index) => {
product.selected = true;
});
In my table HTML I have this:
<tr v-for="(product, index) in this.productList" :data-code="product.code">
<td class='selected'>
<input type="checkbox" :name="'selected'+index" v-model="product.selected">
</td>
etc.
So the checkboxes use 'product.selected' as the model, and this has been set to true for each item in the array, hence the checkbox for each line is initially checked. I can click on the checkbox and it updates the underlying 'product.selected' property accordingly. All well and good so far.
My problem is with using a 'toggleSelection' function, fired by clicking a button, which is intended to check or uncheck all the checkboxes:
toggleSelection(){
this.allSelected = !this.allSelected; //initially set to true in data object
_.each(this.productList, (product, index) => {
product.selected = this.allSelected;
});
},
This would appear to do more or less the same as the initial AJAX call, i.e., loop through the productList array and set the 'selected' property of each product object in the array. And I can see from using the Vue dev tools in Chrome that it is doing exactly this, setting 'product.selected' to either true or false. The problem is, though, that it does not update the user interface - the checkboxes all remain checked even though the property each has been bound to has been changed from true to false.
This doesn't make any kind of sense to me. Why are the checkboxes not getting unchecked when the bound objects are changed?
productList
or any of the elements in that array anywhere else? How do you initially setproductList
? It's not a computed property, right? – thanksd