1

i am trying to add some values. The problem is if one field is empty, the total calculation doesn't show. How can i solve this problem. all my fields type is number. and i am using vue js

grand_total: function(){
                let x = parseInt(this.formData.total_allowance) + parseInt(this.formData.air_fair);
                this.formData.grand_total = x;
                return x;
            }

here. if one value is empty,the total doesnt show up

3 Answers 3

3

Instead of parseInt() you can use Number() as this will convert the empty string as 0 where as you will get NaN with parseInt():

let x = Number(this.formData.total_allowance) + Number(this.formData.air_fair);
Sign up to request clarification or add additional context in comments.

Comments

2

try this one

let x = parseInt(this.formData.total_allowance || 0) + parseInt(this.formData.air_fair || 0)

Comments

1

Use the logical 'or' operator in this way:

grand_total: function() {
   let x = (parseInt(this.formData.total_allowance) || 0) + (parseInt(this.formData.air_fair) || 0);
   this.formData.grand_total = x;
   return x;
}

1 Comment

If you are processing an unknown number of fields you can simply create a separate function that does this same thing for you and returns the appropriate value.

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.