0

l have this json response and want to populate "name" with drop down Vue

{
    "code": 200,
    "message": "OK",
    "payload": [
        "grade",
        [
            {
                "id": 1,
                "name": "B",
                "desc": "test test test"
            }
            {
                "id": 2,
                "name": "B",
                "desc": "test test test"
            }
           
        ]
    ]
}
1
  • 1
    what have you tried so far ? what difficulties did you encounter ? no one is going to write the whole code for you Commented Mar 13, 2021 at 15:46

1 Answer 1

2

Do you want to achieve something like this?

You can do that if you use a v-for and fill the values dynamically in the select option. So no matter what your payload looks like you can put the contents in your dropdown.

Vue.createApp({
  data() {
    return {
     selectedName: '',
     payload: [
        [
         {
           "id": 1,
           "name": "John Doe"
         },
         {
            "id": 2,
            "name": "Elvis Presley"
         }  
      ]
    ]
  }
 }
}).mount('#select-name')
.demo {
  font-family: sans-serif;
  border: 1px solid #eee;
  border-radius: 2px;
  padding: 20px 30px;
  margin-top: 1em;
  margin-bottom: 40px;
  user-select: none;
  overflow-x: auto;
}
<script src="https://unpkg.com/vue@next"></script>

<div id="select-name" class="demo">
  <select v-model="selectedName">
    <option value="" disabled hidden>select a name</option>
    <option v-for="user in payload[0]" :value="user.name">
      {{ user.name }}
    </option>
  </select>
  <br>
  <span>name: {{ selectedName }}</span>
</div>

Look at the official example to understand what is happening there.

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

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.