Vue.js dynamic dropdown

18,340

Try this.

new Vue({
    el: '#app',
    data: {
        filters: filters,
        selectedValue: null
    }
})

<div id="app">
     <select v-model="selectedValue">
         <option disabled value="">Please select one</option>
         <option v-for="item in filters" :value="item">{{item}}</option>
     </select>
</div>

Example.

Note: For future readers, there was also an issue where the normal delimiters for text interpolation needed to be customized in @captnvitman's evnironment.

Share:
18,340
captnvitman
Author by

captnvitman

Updated on July 24, 2022

Comments

  • captnvitman
    captnvitman almost 2 years

    How would I make a dynamic dropdown in vue, not sure what I am doing wrong.

    in my html I have...

    <div id="app">
         <select v-model="selected">
                 <option disabled value="">Please select one</option>
                 <option v-for="item in selected"></option>
         </select>
    

    and my js looks like....

    new Vue({
            el: '#app',
            data: {
              selected: ["Apache", "Cochise"],
            }
          })
    

    filters looks like this enter image description here

    EDIT: the values appear in the html DOM tree in the inspector enter image description here

    but not in the dropdown enter image description here

  • captnvitman
    captnvitman about 7 years
    that work mostly, except for the options do not show in the dropdown, I edited my question with pictures to show what I mean
  • Bert
    Bert about 7 years
    @captnvitman where does filters come from? And can you link where you are seeing this? It works in the example.
  • Bert
    Bert about 7 years
    @captnvitman possibly a version issue? I am using Vue 2 in the example.
  • captnvitman
    captnvitman about 7 years
    from an ajax call, however I changed it to filters: ['Apache','Cochise'] like in your example to keep it simple. I'll edit my question to reflect that
  • captnvitman
    captnvitman about 7 years
    I switched to the same version to check, still the same problem. perhaps its another library im using like bootstrap that is messing with it
  • Bert
    Bert about 7 years
    @captnvitman It looks like you forgot to add {{item}} inside the <option></option>. If you look at your screenshot, there is a value but no text.
  • captnvitman
    captnvitman about 7 years
    i have it like this <option v-for="item in filters" :value="item">{{item}}</option> ... still no luck
  • Bert
    Bert about 7 years
  • captnvitman
    captnvitman about 7 years
    for anyone looking at this in the future with the same problem, it was a problem with the delimiters (maybe getting confused with the django templating?) set delimiters: ['${', '}'], in new Vue () and use ${} for vue templating. thanks to @Bert Evans