I have an applicated that requires the update of two fields that depend on each other for their values.
For example:
<template>
<tr>
<td>{{total}}</td>
<td><input type="text" v-model="calculateEarnedPercentage" @change="updatedForecastPercentage"></td>
<td><input type="text" v-model="spent_dollar"></td>
</tr>
</template>
<script>
export default {
data () {
return {
total: 1000000,
spent_percentage: '',
spent_dollar: 20000,
}
},
methods: {
updatedForecastPercentage () {
this.vendor.regional_forecast_dollar = this.vendor.purchases / (this.vendor.regional_forecast_dollar / 100)
}
},
computed: {
calculateEarnedPercentage () {
return (this.vendor.regional_forecast_dollar / this.vendor.purchases) * 100
}
}
}
</script>
The two "spent" values depend on a static "total" value. I will be storing the spent_dollar, and the percentage will be initially derived from that.
Now if the user updates percentage, I need the dollar value to update. If they update the dollar value, I need the percentage to update.
As of now it obviously doesn't work. Circular updates are happening. How do you design your data to allow this functionality in Vue.js?