0

I'm new to VueJS, I want to ask how can I update the money when the model changed (sum of money and model) then return back the money to html.

<div class="mb-4" v-for="(item, index) in persons" :key="index">
    <div class="flex items-center justify-between">
        <label :for="'person-'+item.id">{{ item.name }}</label>
        <p>{{ item.money }}</p>
    </div>
    <input v-model="item.model" :id="'person-'+item.id" type="number">
</div>

let app = new Vue({
    el: '#app',
    data() {
        return {
            persons: [{
                id: 1,
                name: 'John',
                money: 1000,
                model: ''
            },
            {
                id: 2,
                name: 'Alex',
                money: 5000,
                model: ''
            },
            {
                id: 3,
                name: 'Katie',
                money: 2500,
                model: ''
            }]
        }
    },
    watch: {
        'persons.model'(newVal, oldVal) {
            console.log(newVal)
        }
    }
});

This is my codepen: https://codepen.io/LinhNT/pen/abOymrg

Thank you.

2 Answers 2

1

You can bind an event to change and pass an argument to it:

<input v-model="item.model" @change="(e) => updateMoney(e, item) :id="'person-'+item.id" type="number">

And then make said function updateMoney:

updateMoney(e, item) {
    item.money = e.target.value
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can get the desire result by an Event try this.

<input v-model.number="item.model" @input="item.money = item.model" class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" :id="'person-'+item.id" type="number">

1 Comment

So can I sum total it. Like @input="item.money = item.model + item.money"

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.