I have an input:
<input type="text" id="name" class="form-control" name="name" v-model="form.name" :disabled="validated ? '' : disabled">
and in my Vue.js component, I have:
..
..
ready() {
this.form.name = this.store.name;
this.form.validated = this.store.validated;
},
..
validated
being a boolean
, it can be either 0
or 1
, but no matter what value is stored in the database, my input is always disabled.
I need the input to be disabled if false
, otherwise it should be enabled and editable.
Update:
Doing this always enables the input (no matter I have 0 or 1 in the database):
<input type="text" id="name" class="form-control" name="name" v-model="form.name" :disabled="validated ? '' : disabled">
Doing this always disabled the input (no matter I have 0 or 1 in the database):
<input type="text" id="name" class="form-control" name="name" v-model="form.name" :disabled="validated ? disabled : ''">
You can manipulate
:disabled
attribute in vue.js.It will accept a boolean, if it's true, then the input gets disabled, otherwise it will be enabled...
Something like structured like below in your case for example:
Also read this below:
you could have a computed property that returns a boolean dependent on whatever criteria you need.
then put your logic in a computed property...
Your disabled attribute requires a boolean value:
<input :disabled="validated" />
Notice how i've only checked if
validated
- This should work as0 is falsey
...e.g0 is considered to be false in JS (like undefined or null)
1 is in fact considered to be true
To be extra careful try:
<input :disabled="!!validated" />
This double negation turns the
falsey
ortruthy
value of0
or1
tofalse
ortrue
don't believe me? go into your console and type
!!0
or!!1
:-)Also, to make sure your number
1
or0
are definitely coming through as a Number and not the String'1'
or'0'
pre-pend the value you are checking with a+
e.g<input :disabled="!!+validated"/>
this turns a string of a number into a Number e.g+1 = 1 +'1' = 1
Like David Morrow said above you could put your conditional logic into a method - this gives you more readable code - just return out of the method the condition you wish to check.To remove the disabled prop, you should set its value to
false
. This needs to be the boolean value forfalse
, not the string'false'
.So, if the value for
validated
is either a 1 or a 0, then conditionally set thedisabled
prop based off that value. E.g.:Here is an example.
Can use this add condition.
Not difficult, check this.
jsfiddle