0

I'm just getting started with vue.js and I'm having a hard time figuring out how to attach models to form elements. I have a form with "addons" that looks like this:

    <div id="example">
        <input type="checkbox" name="option1" value="10" v-model="addons">
        <select name="option2" v-model="addons">
            <option value="5" />
            <option value="10 />
            <option value="15" />
        </select>
        <input type="radio" name="option3" value="10" v-model="addons">
        <input type="radio" name="option3" value="20" v-model="addons">

        <div>{{total}}</div>
    </div>

I'm trying to get the sum of all the selected addons. This means that if the user checks the checkbox, selects the second option, and selects the first radio button, the last div should display "30". The form is being generated on the server and so I have no easy way of knowing which type of controls will be present.

This is what I have so far on the javascript side, but it's not working:

 new Vue({
    el: '#example',
    data: {
        addons: []
    },
    computed: {
        total: function() {
            return this.addons.reduce(function(sum, addon) {
                return sum + addon;
            }, 0);
        }
    }
})

What am I missing?

3
  • 1
    what is that extra variable there? where did it came from? Commented Apr 18, 2016 at 11:48
  • Your variable is called addons but you are trying to sum this.extras Commented Apr 18, 2016 at 12:29
  • Sorry, I missed a variable while trying to simplify the example. Fixed. Commented Apr 18, 2016 at 14:47

1 Answer 1

1

You can bind multiple checkboxes to the same variable, and it will create an array. However, you can't model multiple types of inputs to the same variable. You'd need to bind the select to a different variable and the radio buttons to a different variable, then sum those.

Fiddle: https://jsfiddle.net/69dty5a1/

 new Vue({
    el: '#example',
    data: {
        addons: 10,
        select:5,
        radio:10
    },
    computed: {
        total: function() {
            return parseInt(this.addons) + parseInt(this.radio) + parseInt(this.select);
        }
    }
})
Sign up to request clarification or add additional context in comments.

2 Comments

That works, but I'm trying to find a more dynamic solution. The form elements are being generated on the server side and so I don't have an easy way to know what types of controls will be present. I was hoping there was an easy way to attach the model to all the inputs.
I guess you'll need to provide more information about how the inputs are being generated and what they might look like, because I don't know how dynamic you need it to be. Hopefully this helps explain the issue with v-model at least...

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.