Reputation: 150
I'm trying to format number i put in input, i call a function to do this when i left the input but the value is not updated in the v-model.
The function works fine because i alert the value but it never updated in view.
Any idea?
html
<div v-for="year in years">
<input type="text" :disabled="budget.checked[year] == true" v-on:blur="formatMoney(budget.personnelBudget[year])" v-model="budget.personnelBudget[year]"/>
<input type="text" :disabled="budget.checked[year] == true" v-on:blur="formatMoney(budget.travellingBudget[year])" v-model="budget.travellingBudget[year]" />
<input type="text" :disabled="budget.checked[year] == true" v-on:blur="formatMoney(budget.inventoriableBudget[year]" v-model="budget.inventoriableBudget[year]" />
.....
js
data: function(){
return{
budget:{
personnelBudget:[],
travellingBudget:[],
inventoriableBudget:[],
consumablesBudget:[],
indirectExpensesPercent:[],
indirectExpensesBudget:[],
totalBudget:[],
checked:[],
},
},
methods: {
formatMoney(input) {
this.budget.personnelBudget[year]=this.budget.personnelBudget[year]
.replace(/,/g, "")
this.budget.personnelBudget[year]=parseFloat(this.budget.personnelBudget[year])
.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
alert(this.budget.personnelBudget[year])
},
Upvotes: 2
Views: 1506
Reputation: 1
You've a reactivity issue because you're assigning a value to a nested field which is not reflected in template , to solve this try to use this.$set
:
this.$set(this.budget,'personnelBudget',
{...this.budget.personnelBudget,
[year]:this.budget.personnelBudget[year].replace(/,/g, "")})
then try try pass the input as string :
<input ... v-on:blur="formatMoney('personnelBudget',year)" v-model="budget.personnelBudget[year]"/>
and
formatMoney(input,year) {
this.$set(this.budget,'personnelBudget',
{...this.budget[input],
[year]:this.budget[input][year].replace(/,/g, "")})
Upvotes: 2