0

I have created a table which display the product Price, Quantity, Cost, Total If quantity is changed for any product in the table row the value of total also have to update according to the formula quantity * cost. The products array contains cost and name.Everything is working fine but what i don't know is if i am changing quantity of one product all the products in v-for loop getting affected and all products quantity is changing, instead of this how can i change quantity for only updated product and I provided default quantity value as 0 for all products hence only products with more than 0 quantity i can use for my further operations

## Html Code ## 
<table class="table table-striped">
          <thead>
            <tr>
              <td>S.No#</td>
              <td>Product</td>
              <td>Cost</td>
              <td>Quantity</td>
              <td>Total</td>
            </tr>
          </thead>
          <tbody>
            <tr v-for="p in products">
              <td>1</td>
              <td>{{p.item}}</td>
              <td>{{p.cost}}</td>
              <td><input type="number" class="form-control qty-box" name="" v-model='qty'></td>
              <td>{{p.cost*qty}}</td>
            </tr>
          </tbody>
        </table>
## Vue Js code ##
<script>
export default {
  name: 'app',
  data () {
    return {
      counter:8,
      qty:0,
      products:[
      {'item':'timber','cost':250,'id':1},
      {'item':'wood','cost':240,'id':2},
      {'item':'primer','cost':120,'id':3},
      {'item':'plywood','cost':360,'id':4},
      {'item':'marker','cost':220,'id':5},
      {'item':'roughwood','cost':480,'id':6},

      ],
      msg: 'Counter',
    }
  }
}
</script>
1
  • please create live demo or snippet for this ? Commented Aug 2, 2018 at 9:21

3 Answers 3

1

You can't use one number for all rows.

    <table class="table table-striped">
          <thead>
            <tr>
              <td>S.No#</td>
              <td>Product</td>
              <td>Cost</td>
              <td>Quantity</td>
              <td>Total</td>
            </tr>
          </thead>
          <tbody>
            <tr v-for="p in products">
              <td>1</td>
              <td>{{p.item}}</td>
              <td>{{p.cost}}</td>
              <td><input type="number" class="form-control qty-box" name="" v-model='p.qty'></td>
              <td>{{p.cost*p.qty}}</td>
            </tr>
          </tbody>
        </table>

So instead of v-model='qty' use v-model='p.qty' and also instead of this {{p.cost*qty}} use {{p.cost*p.qty}}

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

Comments

0

Follow my example in codepen or the code below:

HTML:

<table id="app" class="table table-striped">
      <thead>
        <tr>
          <td>S.No#</td>
          <td>Product</td>
          <td>Cost</td>
          <td>Quantity</td>
          <td>Total</td>
        </tr>
      </thead>
      <tbody>
        <tr v-for="p in products">
          <td>1</td>
          <td>{{p.item}}</td>
          <td>{{p.cost}}</td>
          <td><input type="number" class="form-control qty-box" name="" v-model='p.qt'></td>
          <td>{{p.cost*p.qt}}</td>
        </tr>
      </tbody>
    </table>

JavaScript:

vue = new Vue({
 el: "#app",
  name: 'app',
  data () {
    return {
      counter:8,
      products:[
      {'item':'timber','cost':250,'id':1, 'qt':1},
      {'item':'wood','cost':240,'id':2, 'qt':1},
      {'item':'primer','cost':120,'id':3, 'qt':1},
      {'item':'plywood','cost':360,'id':4, 'qt':1},
      {'item':'marker','cost':220,'id':5, 'qt':1},
      {'item':'roughwood','cost':480,'id':6, 'qt':1},

      ],
      msg: 'Counter',
    }
  }
});

A little suggestion: Personally, I think, using v-text in HTML with Vue is better than brackets because when using brackets the page will show those before vue parses it which isn't very pretty. IMHO.

1 Comment

Notice that I've added qt parameter to all of your products. Also, see my edited answer
0

Add quantity to each product. You have one quantity value for all products now.

1 Comment

i have used only one quantity value because the products are iterated by v-for loop

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.