6

I have the following code, every time the condition is true, the disabled is affecting all of the buttons in all the items.

I have searched and couldn't find the proper way of doing it, can someone shed some light please?

The purpose of this is to limit the number of an item that the user can add to the shopping basket by disabling the + button.

            <tbody v-for="(item, index) in getMenuItems" :key="index">
                <tr>
                    <td><strong>{{ item.name }}</strong></td>
                </tr>
                <tr v-for="(option, index) in item.options" :key="index" >
                    <td>{{ option.size }}</td>
                    <td>{{ option.price}}</td>
                    <td >
                        <button 
                            class="btn btn-sm btn-outline-success"
                            type="button"
                            @click="addToBasket( item, option)"
                            :disabled="itemdisabled=='disabled'"
                        >+</button>
                    </td>
                        {{ basket }}
                </tr>
            </tbody>
5
  • How do you assign value to itemdisabled? Commented Jun 15, 2018 at 8:23
  • Maybe you could use a computed property for itemDisabled? Commented Jun 15, 2018 at 8:25
  • it looks like you want item.disabled instead of itemdisabled Commented Jun 15, 2018 at 8:29
  • I have a method in 'addToBasket' which tracks the number of items added to the basket like so: this.basket[this.counter].quantity > this.itemMaxQty ? this.basket[this.counter].disabled = "disabled" : this.basket[this.counter].disabled = ""; Commented Jun 15, 2018 at 9:15
  • Here's the vue file: jsfiddle.net/rmy1x3a9 Commented Jun 15, 2018 at 9:23

1 Answer 1

7

You can use computed or methods to check disabled.

For example:

<tbody v-for="(item, index) in getMenuItems" :key="index">
  <tr>
      <td><strong>{{ item.name }}</strong></td>
  </tr>
  <tr v-for="(option, index) in item.options" :key="index" >
      <td>{{ option.size }}</td>
      <td>{{ option.price}}</td>
      <td >
          <button 
              class="btn btn-sm btn-outline-success"
              type="button"
              @click="addToBasket( item, option)"
              :disabled="isDisable(option, index)"
          >+</button>
      </td>
          {{ basket }}
  </tr>
</tbody>

<script>
  export default {
    ...
    methods: {
      isDisable(option, index) {
        // check option and index
        // return true - disable, false - active
      }
    }
  }
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

My disable logic works, but the problem is due to the v-for, it disables all of the buttons in the table.
That's why you need to check isDisable(option, index), base on option and index, you only return true on item you want to be disabled
Hey thanks @ittus, I got it working, but is it an efficient way of doing it having to loop through the basket on every render? jsfiddle here

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.