Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

vue.js - vuejs radio button component

I trying to make this custom radio button component to work in vuejs. How do I make the radio button checked with a value from parent component. I know you use v-model and set it to a same value in one of the input values, but it dont seem to get it work.

component:

export default Vue.component('radioButton', {
  template,
  props: ['name', 'label', 'id', 'value']
})

component template:

<label class="radio" :for="id">
  <input type="radio" :id="id" class="radio-button" :value="value" :name="name">
   <span class="radio-circle">
  </span>
  <span class="radio-circle__inner">
  </span>
  <span class="radio-button__label">{{ label }}</span>
</label>

html:

<radio-button name="options" id="option-1" label="1" :value="selectedValue" />
<radio-button name="options" id="option-2" label="2" :value="selectedValue" />
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For radio buttons, you need to pass true or false for the checked property to preset it to some state. Alternatively, your value in v-model should be equal to value of radio button so that it gets checked.

In the limited sample code you have posted, I believe your label is the button index like 1, 2, 3 and so on... And I think you want to select one of the buttons when the selectedValue matches label of that radio button. For example, if selectedValue is 2, then you want radio button 2 to be checked.

Assuming the above is correct, you need to make a one line change in your radio-button component template:

<input type="radio" class="radio-button" :value="label" :name="name" v-model="value">

Note:

  1. Your button label is the value for radio button. This is what you would expect to set to selectedValue when you click a particular radio button.

  2. Your value in child component is actually selectedValue of parent component, which indicates the radio button that is currently chosen. So this should go into v-model

So, as per the docs on Form Input Bindings, your radio button will get checked if the v-model variable is equal to the value of that radio button.

But now here is another problem: If you click on another radio button, you expect the selectedValue in parent component to change. That is not going to happen because props gives you one-way binding only.

To resolve this issue, you need to do a $emit from your child component (radio button) and capture it in the parent component (your form).

Here is a working jsFiddle example: https://jsfiddle.net/mani04/3uznmk72/

In this example, your form template defines radio-button components as follows:

<radio-button name="options" label="1" :value="selectedValue" @change="changeValue"/>
<radio-button name="options" label="2" :value="selectedValue" @change="changeValue"/>

Whenever the value changes inside child component, it will pass a "change" event along with the label of radio button, which gets passed to changeValue() method of the parent form component. Once the parent component changes selectedValue, your radio buttons update automatically.

Hope it helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...