Reputation: 571
Here my code :
<input
v-model="comb.inactive"
type="checkbox"
@click="setInactive(comb.id_base_product_combination)"
>
I need to apply the invert of the comb.inactive on the v-model.
Here what i tried :
<input
v-model="comb.inactive == 1 ? 0 : 1"
type="checkbox"
@click="setInactive(comb.id_base_product_combination)"
>
<input
v-model="comb.inactive == 1 ? false : true"
type="checkbox"
@click="setInactive(comb.id_base_product_combination)"
>
Do you have others ideas ?
Upvotes: 2
Views: 5178
Reputation: 3240
You can customize the event and property of checkbox instead of using v-model.
<input
class="form-check-input"
type="checkbox"
:id="day.label"
:checked="!day.is_closed"
@change="($event) => day.is_closed = !day.is_closed"
/>
For example in my case, I have a checkbox HTML element.
so I set checked
and change
property.
<input>
with text types and <textarea>
elements use value
property and input
event;<input type="checkbox">
and <input type="radio">
use checked
property and change
event;<select>
use value
as a prop and change
as an event.Upvotes: 0
Reputation: 11807
If you want to invert the v-model, then just make a custom input component!
checkbox-inverted
Vue.component('reverse-checkbox', {
props: ['value', 'label'],
template: `
<label>
<input
type="checkbox"
:checked="valueInverse"
@input="onInputChanged"
/>
<span>{{label}}</span>
</label>
`,
computed: {
valueInverse() {
return !this.value;
},
},
methods: {
onInputChanged(e) {
this.$emit('input', this.valueInverse);
},
},
});
Then you can use it with v-model
like any other input component. more information about custom inputs here
<reverse-checkbox
v-model="invertedModel"
label="This checkbox will show the inverted value"
></reverse-checkbox>
Upvotes: 1
Reputation: 6968
Consider using true-value
and false-value
as shortcuts:
<input
v-model="comb.inactive"
type="checkbox"
:true-value="false"
:false-value="true"
>
Or in this case, where you may be looking for alternating integers, you can just set them directly.
<input
v-model="comb.inactive"
type="checkbox"
:true-value="1"
:false-value="0"
>
Upvotes: 6
Reputation: 28424
You should do the following:
<input
v-model="comb.inactive"
type="checkbox"
@click="setInactive(comb.id_base_product_combination)"
>
mounted(){
this.comb['inactive'] = !(this.comb['inactive']);
}
For better practice, you can use computed
:
<input
v-model="checkedItem"
type="checkbox"
@click="setInactive(comb.id_base_product_combination)"
>
computed: {
checkedItem: {
get: function () {
return !this.comb['inactive'];
},
set: function (newVal) {
console.log("set as you want")
}
}
Upvotes: 2