2

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])
                                            
      },

1 Answer 1

2

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, "")})
Sign up to request clarification or add additional context in comments.

4 Comments

ok i works fine! but to do that for others input in the same method is it correctly pass the v-model in :blur like: v-on:blur="formatMoney(budget.personnelBudget, year) ? the idea is to do that in every input passing the id and year
I don't understand your new issue
i updated my post, you can see i need to do that for others input, just the same
Now is perfect, thanks! i just change the 'personnelBudget' in set for input to do that dinamicaly for others input

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.