Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue.js - Select / dropdown selected item vm binding is not working (bootstrap-vue)

I'm trying to create a simple vue that binds the selected item from a select/dropdown to a property in the vm.

I haven't been able to find a clear and simple example of how this is down when using an options collection that is also in the view model.

<template>
    <div>
        <h1>Select box</h1>
        <b-dropdown id="ddCommodity"
                    name="ddCommodity"
                    v-model="ddTestVm.ddTestSelectedOption"
                    text="Select Item"
                    variant="primary"
                    class="m-md-2" v-on:change="changeItem">
            <b-dropdown-item disabled value="0">Select an Item</b-dropdown-item>
            <b-dropdown-item v-for="option in ddTestVm.options":selected="option.value == 'LME/ST_TNI_ALL'":value="option.value">{{option.text}}</b-dropdown-item>           
        </b-dropdown> <span>Selected: {{ ddTestVm.ddTestSelectedOption }}</span>
 </div>
</template>


<script>
    export default {
        components: {

        },
        data() {
            return {
                someOtherProperty: null,
                ddTestVm: {
                    originalValue: [],
                    ddTestSelectedOption: "Value1",
                    disabled: false,
                    readonly: false,
                    visible: true,
                    color: "",
                    options: [
                        {
                            "value": "Value1",
                            "text": "Value1Text"
                        },
                        {
                            "value": "Value2",
                            "text": "Value2Text"
                        },
                        {
                            "value": "Value3",
                            "text": "Value3Text"
                        }
                    ]
                }
            }
        },        
        methods: {
            changeItem: async function () {
            //grab some remote data
                try {
                    let response = await this.$http.get('https://www.example.com/api/' + this.ddTestVm.ddTestSelectedOption + '.json');
                    console.log(response.data);
                    this.someOtherProperty = response.data;
                } catch (error) {
                    console.log(error)
                }
            }
        },
        watch: {

        },
        async created() {

        }
    }
</script>

<style>
</style>

Regardless of what i've tried i cannot get the selected value in the dropdown to change the ddTestSelectedOption property of the vm.

Could anyone assist on this issue?

Thanks.

like image 819
billy jean Avatar asked Nov 05 '17 17:11

billy jean


People also ask

Why is my dropdown menu not working Bootstrap?

Why is my drop down menu not working in bootstrap? Solution : The dropdown should be toggled via data attributes or using javascript. In the above program, we have forgotten to add a data attribute so the dropdown is not working. So add data-bs-toggle="dropdown" to toggle the dropdown.

How do I get the selected value of dropdown in Vue?

Get Selected Value of Select Dropdown in VueCreated a select box inside the template syntax. Added an onChange() event handler. Created an options list, cars name primarily. Used the on-change method to grab the selected value using the event object.

Can I use Bootstrap-Vue with vue3?

At the time of this writing, Bootstrap-Vue doesn't support Vue 3 but BootstrapVue v4 will have Vue. js v3 support and Bootstrap v5 support according to this issue on GitHub.

Does Bootstrap work with Vuejs?

BootstrapVueWith BootstrapVue you can build responsive, mobile-first, and ARIA accessible projects on the web using Vue.js and the world's most popular front-end CSS library — Bootstrap v4. Bootstrap v4 is the world's most popular framework for building responsive, mobile-first sites.


3 Answers

b-dropdown in bootstrap-vue does not support v-model. As the documentation states:

Dropdowns are toggleable, contextual overlays for displaying lists of links and actions in a dropdown menu format.

In other words, b-dropdown is essentially a UI component for displaying a menu or similar set of options.

I expect what you want is b-form-select.

That said, you could add a click handler to the options that sets the value.

  <b-dropdown-item v-for="option in ddTestVm.options" 
                    :key="option.value" 
                    :value="option.value"
                    @click="ddTestVm.ddTestSelectedOption = option.value">

Here is a working example.

like image 198
Bert Avatar answered Nov 11 '22 16:11

Bert


I thing you need b-form-select

<template>
  <div>
    <b-form-select v-model="selected" :options="options"></b-form-select>
    <b-form-select v-model="selected" :options="options" size="sm" class="mt-3"></b-form-select>

    <div class="mt-3">Selected: <strong>{{ selected }}</strong></div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        selected: null,
        options: [
          { value: null, text: 'Please select an option' },
          { value: 'a', text: 'This is First option' },
          { value: 'b', text: 'Selected Option' },
          { value: { C: '3PO' }, text: 'This is an option with object value' },
          { value: 'd', text: 'This one is disabled', disabled: true }
        ]
      }
    }
  }
</script>
like image 36
Farshid Avatar answered Nov 11 '22 18:11

Farshid


Only b-form-select can achieve the selected value behaviour.

Non-Selected Value Preview: enter image description here

Selected Value Preview: enter image description here

Sample Code:

<template>
  <div>
    <b-form-select v-model="selected" :options="options"></b-form-select>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        selected: null,
        options: [
          { value: 1, text: 'Please select an option' },
          { value: 2, text: 'This is First option' },
          { value: 3, text: 'Selected Option' }
        ]
      }
    }
  }
</script>
like image 1
Jerry Chong Avatar answered Nov 11 '22 16:11

Jerry Chong