1

Quick question. If I have data in an element in the form of an array as follow:

   data: {

     product: socks,

     variants: [
      {
    variantId: 2234,
    variantColor: 'Green',
    variantQuantity: 0,
  },
  {
    variantId: 2235,
    variantColor: 'Blue',
    variantQuantity: 10,
  }

}

how can I select a certain variantQuantity based on me hovering over a certain div?

Full code:

HTML:

<div class="product-image">
  <img :src="image" />
</div>

<div class="product-info">
  <h1>{{ product }}</h1>
  <p v-if="inStock">In Stock</p>
  <p v-else :class="{ outOfStock: !inStock }">Out of Stock</p>

  <ul>
    <li v-for="detail in details">{{ detail }}</li>
  </ul>


    <div class="color-box"
         v-for="variant in variants" 
         :key="variant.variantId"
         :style="{ backgroundColor: variant.variantColor }"
         @mouseover="updateProduct(variant.variantImage)"
         >
    </div> 

    <button v-on:click="addToCart" 
      :disabled="!inStock"
      :class="{ disabledButton: !inStock }"
      >
    Add to cart
    </button>

  <div class="cart">
    <p>Cart({{ cart }})</p>
  </div>

</div>

Javascript:

var app = new Vue({
el: '#app',
data: {
product: 'Socks',
image: 'https://dl.dropboxusercontent.com/s/9zccs3f0pimj0wj/vmSocks-green-onWhite.jpg?dl=0',
inStock: false,
details: ['80% cotton', '20% polyester', 'Gender-neutral'],
variants: [
  {
    variantId: 2234,
    variantColor: 'green',
    variantImage: 'https://dl.dropboxusercontent.com/s/9zccs3f0pimj0wj/vmSocks-green-onWhite.jpg?dl=0',
variantQuantity: 0
  },
  {
    variantId: 2235,
    variantColor: 'blue',
    variantImage: 'https://dl.dropboxusercontent.com/s/t32hpz32y7snfna/vmSocks-blue-onWhite.jpg?dl=0',
variantQuantity: 10
  }
],
cart: 0
},
methods: {
addToCart() {
  this.cart += 1
},
updateProduct(variantImage) {
  this.image = variantImage
}
}
})
5
  • 2
    Please provide more of your code. Commented Oct 6, 2018 at 18:17
  • 1
    Ok, edited with full code Commented Oct 6, 2018 at 19:07
  • what do you mean by ...based on me hovering over a certain div? Commented Oct 6, 2018 at 19:44
  • Right now it's set up so that when I hover over the <div> with the class "colorbox" it changes its image based on which box I hover over. I'd like to make it so that it also displays variantQuantity as well. This is what it looks like in action: codepen.io/VueMastery/pen/ZxWybq?editors=1011 Commented Oct 6, 2018 at 19:50
  • i try to understand you but i couldn't get it Commented Oct 6, 2018 at 20:38

1 Answer 1

1

You could include variant.variantQuantity in the mouseover event-handler expression:

<div v-for="variant in variants" 
     @mouseover="updateProduct(variant.variantImage, variant.variantQuantity)"
     >

Also add a data property for quantity, and update the handler to accommodate the new property:

data() {
  return {
    quantity: 0,
    // ...
  };
},
methods: {
  updateProduct(variantImage, variantQuantity) {
    this.image = variantImage;
    this.quantity = variantQuantity;
  },
  // ...
}

demo based on your codepen

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

Comments

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.