My WORKING piece of code in Vue js:
async mounted() {
const token = this.$store.getters.loggedUser.token;
incidents.getIncidentById(token, this.incidentId).then((resp) => {
this.incidentData = resp.data;
});
incidents.getAllCriteria(token, this.incidentId)
.then(resp => {
this.allCriteria = resp.data;
})
.then(() => {
this.sortAscendingByViolationCriteriumAndFactor();
this.setFactorCheckboxes();
})
.then(() => {
this.allCriteria.forEach(criteria => {
criteria.violationFactors.forEach(factor => {
this.setStateOfTheSameFactorsWithPlusAndMinus(factor);
});
});
});
},
Could you please explain me why the version below version doesn't work? I mean the method setStateOfTheSameFactorsWithPlusAndMinus doesn't work in this case (the method setStateOfTheSameFactorsWithPlusAndMinus is removed from this piece of code and moved to the method setFactorCheckboxes):
async mounted() {
const token = this.$store.getters.loggedUser.token;
incidents.getIncidentById(token, this.incidentId).then((resp) => {
this.incidentData = resp.data;
});
incidents.getAllCriteria(token, this.incidentId)
.then(resp => {
this.allCriteria = resp.data;
})
.then(() => {
this.sortAscendingByViolationCriteriumAndFactor();
this.setFactorCheckboxes();
})
},
methods: {
setFactorCheckboxes() {
this.allCriteria.forEach(criteria => {
criteria.violationFactors.forEach(factor => {
this.selectDegree(factor);
this.setStateOfTheSameFactorsWithPlusAndMinus(factor); //doesn't work here!!!
});
this.updateScoreForCriteria(criteria);
});
}
setStateOfTheSameFactorsWithPlusAndMinus(factor) {
if (factor.factor.includes('(+)') || factor.factor.includes('(-)')) {
let checkbox = document.getElementById('checkbox' + factor.factor);
let factorName = factor.factor;
let factorType = factorName.slice(factorName.length - 3, factorName.length);
let checkboxBasicName = factorName.slice(0, factorName.length - 3);
let checkboxToFindAndChangeState = checkboxBasicName.concat(factorType === '(+)' ? '(-)' : '(+)');
let checkboxToDisable = document.getElementById('checkbox' + checkboxToFindAndChangeState);
if (checkbox.checked) {
checkboxToDisable.disabled = true;
} else {
checkboxToDisable.disabled = false;
}
}
},
It's very weird! "Doesn't work" means that the loaded page behaves very unpredictable - it seems that not all data are loaded and the method setStateOfTheSameFactorsWithPlusAndMinus can't check some checkboxes correctly