69

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?

5 Answers 5

107

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/

Sign up to request clarification or add additional context in comments.

4 Comments

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
@Chris22 Should just be data: function () { return { selected: 'bar' } }
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.
This answer is very helpful for me. I'm using Vue Js 3 and I had the bound value set to ref(0). My list of options started with a value of 1. I overlooked my initial bound variable initialization and kept trying to tweak the "selected" attribute on the html component.
11

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']
    },

2 Comments

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.
@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
3

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)

Comments

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>

1 Comment

The question is tagged Vue2 (although technically the composition API can be used with that version)
0

So I'm not sure if this has been resolved but I just wanted to add another option here.

<select v-model="selectedOffice" class="select-style">
    <option v-for="(office_id) in OID"
    :key="office_id">
    {{ office_id.office_name }}
    </option>
  </select>

JS:

if (this.OID.length < 2) {
    this.selectedOffice = this.OID[0].office_name;
    this.officeID = this.OID[0].office_id;
  }

So in this situation, I set the v-model variable to the first option in the array if there is only one option in the array. The concept though is the same if there are more options.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.