0

I'm getting the following:

enter image description here

enter image description here

Vue template code:

<select class="form-select" v-model="post_format.wpml_language">

<option  v-for="(lang, index) in wpml_languages" :value="lang">{{lang}} {{index}}</option>

</select>

What I want; I want the value attribute of the option element to be the language code (en/es) and the text displayed to be the Language name

I'm not sure how to access the object in the vue code for it to display in the way I want. I've tried:

<select class="form-select" v-model="post_format.wpml_language">
<option  v-for="(lang, index) in wpml_languages" :value="lang[index]">{{lang[index]}}</option> 
</select>

Any help is appreciated

0

2 Answers 2

1

I recommend you to modify your object so that your code and language are separate values.

this.languages.filter(lang =>{
  var code = Object.keys(lang)[0];
  lang.code = code;
  lang.value = lang[code];
});

This will make your code more readable. Codesandbox link for your reference.

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

Comments

0

You probably don't want lang[index], because you have nested objects. In this instance, the actual loop index is not useful. For each member of 'lang', you most likely want the key to be your option value ("en") and the value to be the printed text ("English"). While the following is not the cleanest option, it will work:

<select class="form-select" v-model="post_format.wpml_language">
    <option  v-for="(lang, index) in wpml_languages" :value="Object.keys(lang)[0]">
        {{ lang[0] }}
    </option> 
</select>

1 Comment

The value works but the text for the option is blank

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.