Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue v-model not reactive with BS4 radio button group

I'm hoping I'm just missing something simple because I've been looking at this for too long, but I'm stumped.

I have a form with inputs bound to vuejs. I have a group of 2 radio buttons for selecting the "gender", and the binding is working perfectly. If I click on either of the radio buttons, I can see the data change in the vue component inspector.

But I'm trying to change the radio buttons to a Bootstrap 4 button group, and can't seem to get the v-model binding to work. No matter what I try, the gender_id in my vue data is not getting updated when I click either of the buttons in the button group.

The form input values are being fed in through vue component properties, but for simplicity, my data for the radio buttons/button group would look like this:

export default {
    data() {
        return {
            genders: {
                1: "Men's",
                2: "Women's"
            },
            gender_id: {
                type: Number,
                default: null
            }
        }
    }
}

Here is the code I have for the radio button version (which is working properly):

<div class="form-group">
    <label>Gender:</label>
    <div>
        <div class="form-check form-check-inline" v-for="(gender, key) in genders" :key="key">
            <input type="radio"
                class="form-check-input"
                name="gender_id"
                :id="'gender_' + key"
                :value="key"
                v-model.number="gender_id">
            <label class="form-check-label" :for="'gender_' + key">
                {{ gender }}
            </label>
        </div>
    </div>
</div>

Here is the button group version that is not properly binding to the gender_id data in vue.

<div class="form-group">
    <label>Gender:</label>
    <div>
        <div class="btn-group btn-group-toggle" data-toggle="buttons">
            <label class="btn btn-outline-secondary" v-for="(gender, key) in genders" :key="key">
                <input type="radio"
                    class="btn-group-toggle"
                    name="gender_id"
                    :id="'gender_' + key"
                    :value="key"
                    autocomplete="off"
                    v-model.number="gender_id">
                {{ gender }}
            </label>
        </div>
    </div>
</div>

I've been using the following Boostrap 4 documentation to try to get this working. https://getbootstrap.com/docs/4.0/components/buttons/#checkbox-and-radio-buttons

In the documentation for button groups they don't even include the value property of the radio inputs, whereas they do include it in the documentation for form radio buttons. https://getbootstrap.com/docs/4.0/components/forms/#checkboxes-and-radios

Is this for simplicity or do button groups of radio buttons not even return the value of the checked button?

I see other threads stating that buttons groups are not meant to function as radio buttons, but if that's true for BS4, then why would Bootstrap have button groups with radio buttons as they do in their documentation referenced above? If you can't retrieve the checked state, then why not just use a <button> instead of <label><input type=radio></label>?

Any ideas as to what I'm doing wrong and/or not understanding correctly?

Thanks so much!

like image 766
CICSolutions Avatar asked Mar 21 '18 18:03

CICSolutions


2 Answers

Thanks so much to @ebbishop for his helpful insights.

The issue was related to vue and bootstrap both trying to apply javascript to the buttons in the button group.

To get around this issue, it was as simple as removing data-toggle="buttons" from the button group. By removing the data-toggle attribute, the bootstrap js is not applied and vue can manage the button group.

like image 123
CICSolutions Avatar answered Nov 16 '22 01:11

CICSolutions


Nothing is actually wrong your use of v-model here.

However: you must add the class "active" to the <label> that wraps each radio-button <input>.

See this fiddle for a working example.

Is that what you're after?

like image 30
ebbishop Avatar answered Nov 16 '22 02:11

ebbishop