61

I want to bind a custom attribute to an option select menu. The <option> tag would simply have an attribute of selected="selected"

<template>
  <div>
    <select>
      <option v-for="" v-bind:selected="[condition ? selected : notSelected]" value="">{{ resource.xprm_name }}</option>
    </select>
  </div>
</template>

data: {
  selected: "selected",
  notSelected: ""
}

This does not work, but if I change v-bind:selected to v-bind:class then it receives the appropriate class, so the logic is working, but not with the selected attribute.

Any way to make it work with such custom attributes?

marchello
  • 2,016
  • 3
  • 29
  • 39

4 Answers4

102

You can just use v-model for selecting a default value on a select box:

Markup:

<div id="app">
  <select v-model="selected">
     <option value="foo">foo</option>
     <option value="bar">Bar</option>
     <option value="baz">Baz</option>
  </select>
</div>

View Model:

new Vue({
  el: "#app",
  data: {
    selected: 'bar'
  }
});

Here's the JSFiddle: https://jsfiddle.net/Lxfxyqmf/

craig_h
  • 31,871
  • 6
  • 59
  • 68
  • How can you use this in the `data(){} function`? Nothing I have tried has worked and i have looked through several threads here on SO using several different ways and the VueJs2 docs. [Select Basic Usage](https://vuejs.org/v2/guide/forms.html#Basic-Usage) – Chris22 Aug 06 '18 at 22:09
  • 2
    @Chris22 Should just be `data: function () { return { selected: 'bar' } }` – Quiver Aug 09 '18 at 14:39
  • 11
    How would you achieve this if you wanted to select the first index only. Ie. you don't know the results returned, so just auto-select the first result. – ejntaylor Jun 22 '19 at 08:32
12

html:

<div id="myComponent">
    <select v-model="selectedValue">
        <option v-for="listValue in valuesList" :value="listValue">
            {{listValue}}
        </option>
    </select>
    <span>Chosen item: {{ selectedValue }}</span>
</div>

js:

var app = new Vue({
    el: '#myComponent',
    data: {
        selectedValue: 'Two',
        valuesList: ['One', 'Two', 'Three']
    },
Delphin RUKUNDO
  • 553
  • 5
  • 8
Ilya Kolesnikov
  • 623
  • 8
  • 17
  • 2
    This answer can be considered right when you simply want to have a value selected by default and don't want to use `v-model`. – Gustavo Straube Oct 11 '19 at 17:36
  • 1
    @GustavoStraube Even if you use v-model, I dont see it selecting a default option. Using this along with v-model works in selecting a default – Joel G Mathew Jun 24 '21 at 14:52
2

I have created a reusable component of select that also emits the modelValue to parent component.

If you look at the first option tag, you can see how I create a default value using props.

BaseSelect.vue (child component)

<template>
  <label v-if="label">{{ label }}</label>
  <select
    class="field"
    :value="modelValue"
    v-bind="{
      ...$attrs,
      onChange: ($event) => {
        $emit('update:modelValue', $event.target.value);
      },
    }"
  >
    <option value="" disabled>{{ defaultValue }}</option>
    <option
      v-for="option in options"
      :key="option"
      :value="option"
      :selected="option === modelValue"
    >
      {{ option }}
    </option>
  </select>
</template>

<script>
export default {
  props: {
    label: {
      type: String,
      default: ""
    },
    defaultValue: {
      type: String,
      default: "Select an item of the list",
      required: false
    },
    modelValue: {
      type: [String, Number],
      default: "Otros"
    },
    options: {
      type: Array,
      required: true
    },
  },
};
</script>

Parent component

<BaseSelect
      label="Asunto"
      defaultValue = "Selecciona un asunto" 
      v-model="contactValues.asunto"
      :options="asuntos"
    />

Notice that you have to receipt correctly this value in your data() (v-model)

Arkox
  • 51
  • 2
0

Using the new composition API:

<template>
     <select v-model="selectValue">
         <option value="a">
             Option a
         </option>
         <option value="b">
             Option b
         </option>
      </select>
</template>

<script>
import { ref } from 'vue';

export default {
    setup() {
        // Create a reactive value for our select
        const selectValue = ref('a');

        // Return to the template
        return { selectValue };
    },
};
</script>