2

I don't know how to calculate the quantity in a loop.

 <tr v-for="product in products">
        <td>{{ product.name }}</td>
        <td>
            <span v-for="price in product.prices" v-html=" '&#8369; ' + price.price + ', '"></span>
         </td>
       <td >
           <span v-for="quantity in product.quantities">{{ Need to total quantities here. }}</span>
       </td>
  </tr>

My Data

enter image description here

I need to total all quantities. I highlighted the qty. TY

3
  • 2
    Use a computed property which returns product.quantities.reduce((sum, product) => sum + product.qty, 0). You could also create a filter. See vuejs.org/v2/guide/computed.html Commented May 19, 2017 at 7:07
  • Thanks, I'm still figuring out your solution. Commented May 19, 2017 at 7:23
  • It doesn't work. I think your .reduce will work if the data is look like this. donutData: [ { label: 'Openstaande verzoeken', value: 20 }, { label: 'Geaccepteerde verzoeken', value: 25 }, { label: 'Afgewezen verzoeken', value: 10 } ], My solution is really stupid. I'll post it here. I hope someone can refine the code. Sigh.... Commented May 19, 2017 at 8:28

1 Answer 1

2

There are some variants.

1st

You can add fields to your products array when you get it script

this.products = someData;
this.products.forEach((pr) => {
   pr.totalQuantities = pr.quantities.map((q)=>q.qty).reduce(function(total, q) {
                 return total + q}, 0);
})

template

<tr v-for="product in products">
        <td>{{ product.name }}</td>
        <td>
            <span v-for="price in product.prices" v-html=" '&#8369; ' + 
              price.price + ', '"></span>
         </td>
       <td >
           <span>{{product.totalQuantities}}</span>
       </td>
  </tr>

2nd

Add filter to count total script (insert into vue instance)

filters: {
    total: function (quantities) {
      return quantities.map((q)=>q.qty).reduce(function(total, q) {
                 return total + q}, 0);
    }
  }

template

<tr v-for="product in products">
        <td>{{ product.name }}</td>
        <td>
            <span v-for="price in product.prices" v-html=" '&#8369; ' + 
              price.price + ', '"></span>
         </td>
       <td >
           <span>{{product.quantities | total}}</span>
       </td>
  </tr>

3rd

script

computed: {
   quantities(){
       return products.map(function(pr){
            return pr.quantities.map((q)=>q.qty).reduce(function(total, q) {
                 return total + q}, 0);
       });
   }
}

template

<tr v-for="product, index in products">
        <td>{{ product.name }}</td>
        <td>
            <span v-for="price in product.prices" v-html=" '&#8369; ' + 
             price.price + ', '"></span>
         </td>
       <td >
           <span>{{ quantities[index] }}</span>
       </td>
  </tr>

variant for work with index

script

   quantities(){
       return (index) => {
          return this.$store.getters.products[index].quantities.map((q)=>q.qt‌​y).reduce(function(t‌​otal, q) { return total + q}, 0) }
       }
   }

template

{{quantities(index)}}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks I'll try this one and get back on your answers
There is an error TypeError: Reduce of empty array with no initial value I don't know why. The 1st and the 2nd has the same errors.
I have tried your example and it's working. I'll try this with my real data and get back here. Thanks
It works now... I need to put it in methods because I need to pass an index. quantities: function(index){ return this.$store.getters.products[index].quantities.map((q)=>q.qty).reduce(function(total, q) { return total + q}, 0) } @Kirill Matrosov. Thank you so much...
@Rbex, it is bad idea to put it to method. it willl recount every instance update. U can use computed property and return function in it. Update post
|

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.