menu

Questions & Answers

The radio button <b-form-radio-group> is not checked (turning blue)

I'm using Bootstrap-vue.JS to make a group of radio button. I have a reset function to have one of radio button checked. when I call the function, the value of the radio button is change as I expected, but the radio button itself does not showing its changed (the circle is not turning blue)

here is the template

<b-row md="9" align-h="center">
 <b-form-group>
  <b-form-radio-group
   id="radio-group-1"
   v-model="voc_type"
   name="radio-options"
  >
   <b-form-radio value="Request">Request</b-form-radio>
   <b-form-radio value="Complain">Complain</b-form-radio>
   <b-form-radio value="Saran">Saran</b-form-radio>
   <b-form-radio value="Pujian">Pujian</b-form-radio>
  </b-form-radio-group>
 </b-form-group>
</b-row>
{{ voc_type }}

Here is the initialization when the vue is created

export default{
 data{
  return{
   voc_type: 'Request',
  }
 }
}

Here is the reset function

reset(){
 this.voc_type= 'Request'
}

when I call the reset(), the output of {{ voc_type }} is "Request" as I expected, but the radio button is not turning blue. idk why..

Answers(2) :

I implemented a reset buttons, it works as expected now:

<template>
  <div>
    <b-row md="9" align-h="center">
      <b-form-group>
        <b-form-radio-group id="radio-group-1" v-model="voc_type" name="radio-options">
          <b-form-radio value="Request">Request</b-form-radio>
          <b-form-radio value="Complain">Complain</b-form-radio>
          <b-form-radio value="Saran">Saran</b-form-radio>
          <b-form-radio value="Pujian">Pujian</b-form-radio>
        </b-form-radio-group>
      </b-form-group>
    </b-row>
    {{ voc_type }}
    <b-btn @click="reset()">Reset</b-btn>
  </div>
</template>

<script>
export default {
  data() {
    return {
      voc_type: 'Request'
    };
  },
  methods: {
    reset() {
      this.voc_type = 'Request';
    }
  }
};
</script>

There is a typo in your data function due to this the Vue reactivity probably didn't work correctly

  data() { <-- correct this line 
    return {
      voc_type: 'Request'
    };
  },
Comments:
2023-01-20 23:20:10
sorry i had a typo in that data function, my real code is good. but it's really weird to see my code works now, i haven't make any changes in my code yet.. hmmm :V thanks for the answer tho..

During my development of the nuxt app, I found a very clean way to use the b-form-radio buttons.

<template>
  <div>
    <b-form-group>
      <b-form-radio-group v-model="selectedOption" :options="options" name="name"></b-form-radio-group>
    </b-form-group>
  </div>
</template>

<script>
export default {
  data() {
    return {
      options: [
        { value: 'option1', text: 'Option 1' },
        { value: 'option2', text: 'Option 2' },
        { value: 'option3', text: 'Option 3' }
      ],
      selectedOption: ''
    }
  }
}
</script>