4

The title describes what I want, this is the code

All is fine when we add a product to items if it doesn't exists (difference by id). But if it exists, I want to modify only the item. The difference is made by id of each item in items array.

Eg : if first, id = 1, qty = 3, and next, id = 1, qty = 3, I want the update of qty in items

new Vue({
  el: '#fact',
  data: {
    input: {
      id: null,
      qty: 1
    },
    items: []
  },
  methods: {
    addItem() {
      var item = {
        id: this.input.id,
        qty: this.input.qty
      };
      
      if(index = this.itemExists(item) !== false)
      {
          this.items.slice(index, 1, item);
          return null;
      }
      
      this.items.push(item)
    },
    itemExists($input){
       for (var i = 0, c = this.items.length; i < c; i++) {
           if (this.items[i].id == $input.id) {
               return i;
          }
       }
       return false;
    }
  }
})
<Doctype html>
  <html>

  <head>
    <meta charset="utf-8" />
    <title>Add product</title>
  </head>

  <body>
    <div id="fact">
      <p>
        <input type="text" v-model="input.id" placeholder="id of product" />
      </p>
      <p>
        <input type="text" v-model="input.qty" placeholder="quantity of product" />
      </p>
      <button @click="addItem">Add</button>

      <ul v-if="items.length > 0">
        <li v-for="item in items">{{ item.qty + ' ' + item.id }}</li>
      </ul>
      
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.13/vue.min.js"></script>
  </body>

  </html>

0

2 Answers 2

6

You might have misused the slice method, change slice to splice works for me:

this.items.splice(index, 1, item)

slice doesn't trigger view updates according to the documentation here.

Vue wraps an observed array’s mutation methods so they will also trigger view updates. The wrapped methods are:

  • push()
  • pop()
  • shift()
  • unshift()
  • splice()
  • sort()
  • reverse()
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for this remark! But even modifying, he adds this time the item in question. By cons I want the item already added in items is changed!
By using splice(index, 1, item), you replace the original item with the new item. If you have other fields, you might use Object.assign to create a new item that merge into the original item and then replace it. Something like: splice(index, 1, Object.assign({}, this.items[index], item)).
Directly modifying the item in place won't be a good idea because Vue can't detect the changes you made. See Caveates.
Thank you for your assistance ! after a moment to check what it wasn't work, I discover if(index = this.itemExists(item) !== false) that would be (index = this.itemExists(item)) !== false). With the same code, of course your remark included (splice instand of slice), all is work fine now ! Thank you !
-1
if(index = this.itemExists(item) !== false)
{
    for (let value of this.items) {
        if(value.id === item.id) {
            value.qty = item.qty;
        }
     }

     return null;
 }

2 Comments

The difference is made by id of item (item.id) , do you think realy that it will help me ?
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion

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.