question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

v-model with custom element that wrap checkbox.

See original GitHub issue

Hi,

I am using vuejs 2.0.1 and I want to create a component that looks like this:

    <vue-checkbox v-model="page.ads_able"></vue-checkbox>

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:

...
<input type="checkbox" :value="value" @input="onInput" />
...
...
props: {
        value: {},
},
methods: {
        onInput(event) {
            this.$emit('input', event.target.value)
        }
}
...

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?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

26reactions
phattarachaicommented, Oct 3, 2016

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. 😄

17reactions
arvx47commented, Jun 10, 2020

Sorry for beign late. I came up with this solution, what do you guys think?

Vue.component("v-check", {
  template: `	
  <input type="checkbox" :value="value" v-model="proxyChecked" />
  `,
  model: {
    prop: "checked",
    event: "change",
  },
  props: {
    checked: {
      type: [Array, Boolean],
      default: false,
    },
    value: {
      default: null,
    },
  },
  computed: {
    proxyChecked: {
      get() {
        return this.checked;
      },
      set(val) {
        this.$emit("change", val);
      },
    },
  },
});

Code demo here: https://jsfiddle.net/ariel0196/euzn79gc/3/

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue: binding with v-model in custom checkbox component ...
When you do this.$emit('input', e.target.value);. it works like myCheckBoxModel = e.target.value. So it just assigns the value of the last ...
Read more >
Using v-model with checkbox in custom component - Vue Forum
I'm having trouble figuring out how to properly make use of v-model within a custom component that has a checkbox input. Using v-model...
Read more >
Create a Custom Checkbox Component with Vue - Muhi Masri
In this article I'd like to share with you how to create a reusable custom checkbox element using CSS and VueJS.
Read more >
Vue two way data-binding in custom checkbox
Let's start creating our component. It will be as simple as just rendering the checkbox and a label can be set through props....
Read more >
Creating Custom Inputs With Vue.js - Smashing Magazine
Learn how to create custom checkboxes and radios that emulate how v-model works on them natively. Quick note before we get started: ES2015+...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found