Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am new to Vue.js and I can't seem to figure out how to change the format of data. Currently it's using the following structure.

app.js:

new Vue({
  el: '#app',
  data: {
    price: 0,
    shipping: 0,
    handling: 0,
    discount: 0
  },
  computed: {
    total: function () {
      return ((
        this.price * 100 + 
        this.shipping * 100 + 
        this.handling * 100 - 
        this.discount * 100
      ) / 100).toFixed(2)
    }
  }
})

index.html:

<div id="app" class="row">
  <currency-input label="Price" v-model="price"></currency-input>
  <currency-input label="Shipping" v-model="shipping"></currency-input>
  <currency-input label="Handling" v-model="handling"></currency-input>
  <currency-input label="Discount" v-model="discount"></currency-input>

  <p class="medium-12 columns">Total: ${{ total }}</p>
</div>

http://codepen.io/pixelasticity/pen/rjeVgz

I want to change the data to the following, or some variation thereof, but it doesn't work.

app.js:

new Vue({
  el: '#app',
  data: {
    products: [
      {
        price: 0,
        shipping: 0,
        handling: 0,
        discount: 0
      }
    ]
  },
  computed: {
    total: function () {
      return ((
        this.products[0].price * 100 + 
        this.products[0].shipping * 100 + 
        this.products[0].handling * 100 - 
        this.products[0].discount * 100
      ) / 100).toFixed(2)
    }
  }
})

What am I missing?

share|improve this question
    
Why did you change data to a products array? – Stephen Jan 11 at 22:40
    
So that I could have multiple products with different but still adjustable prices, shipping fees and discounts. I actually want to have the price inputs in a child component. – Confused One Jan 11 at 22:51
up vote 0 down vote accepted

You have to update properties in v-model directives, too.

<currency-input label="Price" v-model="products[0].price"></currency-input>
<currency-input label="Shipping" v-model="products[0].shipping"></currency-input>
<currency-input label="Handling" v-model="products[0].handling"></currency-input>
<currency-input label="Discount" v-model="products[0].discount"></currency-input>

This ansewers your question. However, your data structure seems wired. Why you need a array here while you sum only first product? Your total computed property do not sum all other products anyway.

share|improve this answer
    
Thanks. I knew it had to be something obvious like that. The reason I want it formatted that way is so I can have multiple products with different default prices and shipping fees. – Confused One Jan 11 at 22:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.