-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v-model with custom element that wrap checkbox. #3838
Comments
Well, I think I find a solution to my own question. I need to set a data variable to accept value from props and change that data at the corresponding event then emit the input event so that v-model can handle the value change. Something along these lines: template <input type="checkbox"
value="1"
:checked="checked"
@change="change"
/> And in javascript: data: function() {
return {
checked: this.value
};
},
props: ['value'],
methods: {
change: function() {
this.checked = !this.checked;
this.$emit('input', this.checked);
}
} That's work now. :D |
@phatchai Here is a work around for the issue. |
Issues are for bugs and feature requests only. Please use forum.vuejs.org or Gitter. |
@phatchai Your example works not correctly. When we change parent model - yours checkbox component wouldn't change. Here is my example: <template>
<input type="checkbox" :checked="value" @change="changeHandler">
</template>
<script>
export default {
name: 'v-switch',
props: ['value'],
methods: {
changeHandler () {
this.$emit('input', !this.value)
}
}
}
</script> |
There needs to be better documentation on v-model. Everywhere I look says it's just a shorthand for :input="$emit('input', event.target.value)" but this issue proves otherwise. The bug here is lack of documentation. Don't use any of the other examples here! They are broken! <input type="checkbox" :value="value" @input="$emit('input', $event.target.checked)" /> |
@myxibrium solution didn't work to me but this did (changing <template>
<input type="checkbox" :checked="value" @input="$emit('input', $event.target.checked)" />
</template>
<script>
export default {
props: ['value']
};
</script> |
I can also bind a group of checkboxes to an array. The array will then contain a list of the values of the checked checkboxes, see 2nd example in the documentation https://vuejs.org/v2/guide/forms.html#Checkbox |
https://vuejs.org/v2/api/#model Use a custom model prop and event. Eg.
Bind the |
@vincesp I managed to solve this by using @rjoo 's answer and a bit of array checking when the box change.
codesandbox here: https://codesandbox.io/s/5x0296z7jp I didn't put handling of single select boxes in there, but that's fairly trivial to do. Hope it helps. |
Sorry for beign late. I came up with this solution, what do you guys think?
Code demo here: https://jsfiddle.net/ariel0196/euzn79gc/3/ |
Don't know about Vue 2 but for Vue 3, it's as simple as this. props: {
modelValue: [Boolean, Array]
} <input
type="checkbox"
v-model="modelValue"
@change="$emit('update:modelValue', modelValue)"
> |
Not sure if things have changed since the above was written, but none of the above worked for me. <template>
<input type="checkbox" :checked="modelValue" @change="$emit('update:modelValue', $event.target.checked)" />
</template>
<script>
export default {
props: ['modelValue'],
emits: ['update:modelValue']
}
</script> |
Hi,
I am using vuejs 2.0.1 and I want to create a component that looks like this:
As from the api page that v-model is a syntactic sugar combinding
:value
and@input
.So I write a template with a props name value and a method for input event like this:
But it doesn't work. I don't know if there any special care about checked attribute of the checkbox. I tried the exact same thing with
<select>
and it's worked as aspect though. Any suggestion?The text was updated successfully, but these errors were encountered: