0

How to selected first element from Select if value object ? I have array two level. I need an object in order to pass two parameters (name, value)

<select  v-model="getSelectLanguage" class="uk-select">
   <option :value="sel" v-for="(sel,index) in serviclevel.selectlanguage" >
                                {{sel.label}} 
   </option>
</select>

Example data

Services_level_2: [{
    head_option: "Lorem Ipsum is simply dummy text of the printing and typesetting ",    
    selectlanguage: [{
        label: "1",
        price: 1
      },
      {
        label: "2",
        price: 1.5
      },
      {
        label: "3",
        price: 2.3
      },
      {
        label: "4",
        price: 3.2
      },
      {
        label: "5+",
        price: 4
      }
    ]

  }]

Example demo

2 Answers 2

2

You could initialize selected to the desired default option.

const options = [
   {
      text: 'One',
      value: 'A'
   },
   {
      text: 'Two',
      value: 'B'
   }, 
   {
      text: 'Three',
      value: 'C'
   }
];


new Vue({
  el: '#my-inputs',
  data: {
    selected: options[0],
    options: options
  },
})
Sign up to request clarification or add additional context in comments.

Comments

2

If you want the first item to always be selected you can add :selected="index === 0" to the option element.

jsfiddle

Edit

https://v2.vuejs.org/v2/guide/forms.html#Basic-Usage

v-model will ignore the initial value, checked or selected attributes found on any form elements. It will always treat the Vue instance data as the source of truth. You should declare the initial value on the JavaScript side, inside the data option of your component.

...

If the initial value of your v-model expression does not match any of the options, the element will render in an “unselected” state. On iOS this will cause the user not being able to select the first item because iOS does not fire a change event in this case. It is therefore recommended to provide a disabled option with an empty value, as demonstrated in the example above.

In short, if selected doesn't match an option, it will show the unselected state.

updated fiddle

1 Comment

I tried it, but it does not work for me. Then I will understand further

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.