To start off, make sure to connect values and v-models to your checkboxes:
<input type="checkbox" :id="subregion[0].subregion" v-model="subregionCheck" :value="subregion[0].subregion">
<input type="checkbox" :id="country.name" v-model="countryCheck" :value="country.name">
Additionally, include arrays for subregionCheck and countryCheck in your data:
data: {
subregions: null,
countries: null,
query: '',
countryList: [],
subregionCheck:[],
countryCheck: []
},
These arrays act as indicators for our checkboxes: if they contain the value of a single checkbox, it will be checked. Initially, both arrays are empty.
Next, we need to set up a listener for the subregion checkbox along with a function to check all country checkboxes related to that subregion. Start by adding a click listener to the subregion checkbox:
<input type="checkbox" :id="subregion[0].subregion" v-model="subregionCheck" :value="subregion[0].subregion" @click="checkAllCountries(subregion)">
Then define the method (since ES6 is not used, "this" needs to be assigned to a variable):
checkAllCountries: function (subregion) {
var that = this;
if (this.subregionCheck.indexOf(subregion[0].subregion) > -1) {
subregion.forEach(function (element) {
if (that.countryCheck.indexOf(element.name) <= -1) {
that.countryCheck.push(element.name);
}
});
}
else {
subregion.forEach(function (element) {
that.countryCheck.splice(that.countryCheck.indexOf(element.name), 1);
})
}
},
Now we must create a method to uncheck the subregion checkbox if any of its corresponding countries are unchecked. Add a click listener to the country checkboxes:
<input type="checkbox" :id="country.name" v-model="countryCheck" :value="country.name" @click="checkSubregion(subregion)">
Then define the method:
checkSubregion: function (country) {
if ((this.countryCheck.indexOf(country.name) <= -1) && this.subregionCheck.indexOf(country.subregion) > -1 ) {
this.subregionCheck.splice(this.subregionCheck.indexOf(country.subregion), 1);
}
},
View demo