0

I am trying to compate initial object and updated object specific values so if they are the same button should be disabled. However it seems even after change button is disabled:

  setDisabled(){
  return this.selectedItem.color === this.selectedItemInitial.color &&
  this.selectedItem.price === this.selectedItemInitial.price
  },

What is wrong and why it's not changing boolean value?

0

3 Answers 3

2

The function setDisabled you defined is executed once when the component is rendered but not when the data in the component changes.

You should move setDisabled (and rename it to buttonDisabled for clarity) to the computed properties of the component. This way it will get updated when data or props get updated in the component:

computed: {
  buttonDisabled: function(){
        return this.selectedItem.color === this.selectedItemInitial.color && this.selectedItem.price === this.selectedItemInitial.price
   }
}

and use it like this in the html:

<!-- No parenthesis when using a computed property -->
<button :disabled="buttonDisabled"> ACTION </button>
Sign up to request clarification or add additional context in comments.

2 Comments

Can you show how you assign selectedItem and selectedItemInitial? Vue detect reference changes so you may change the properties of selectedItem but not the object itself and Vue wont detect it. I suspect this error
I assume you are right that is the case and how can be solved in that case?
0

You don't show how you instantiate your component data, so its hard to see if there is a logic error.

In general, that method works, if used like this:

<button :disabled="setDisabled()"> ACTION </button>

also if I suggest you to change it to a computed property instead:

computed: {
  setDisabled: function(){
        //logic here
   }
}

Comments

0

You can also do the whole thing in the template. Depending on how the variables are set.

<button :disabled="selectedItem.color === selectedItemInitial.color">Click</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.