0

Here is my code below

<td v-for="config in configs" v-if="config">
    <input type="number" v-model="config.score" v-on:keyup="computeTestScores(allocation, $event)">
 </td>
<td><span class="subtotal"></span></td>`

how do i compute the subtotal? Secondly, if the values of any input changes, i want to be able update the total class;

2 Answers 2

2

You should not try to compute the sum with v-for. V-for is meant for presentation.

Instead add a computed value that calculates the subtotal. Inside the computed you should loop through the array, calculate the sum and return it. The computed property also calculates the new value automatically if any of the inputs change.

Sign up to request clarification or add additional context in comments.

1 Comment

now the problem is that my v-for loop for configs is inside another v-for loop called allocations. can you do me a quick short code? thanks
0

You may sum the values of your array with the reduce function. The total will update automatically if any score changes.

<td><span class="subtotal">{{ configs.reduce((a, c) => a + parseInt(c.score), 0) }}</span></td>

Comments

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.