9

How do I always select the first item with a select (in vue.js)?

<div class="ride-item-content">
  <select v-model="ride.location_id">
    <option v-for="location in locations" v-bind:value="location.id">
      {{ location.from }} - {{ location.to }}
    </option>
  </select>
</div>

json

{
  "locations": {
    "total": 2,
    "per_page": 20,
    "current_page": 1,
    "last_page": 1,
    "next_page_url": null,
    "prev_page_url": null,
    "from": 1,
    "to": 2,
    "data": [
      {
        "id": 1,
        "user_id": 1,
        "description": "sdg",
        "from": "asdf",
        "to": "asdf",
        "kmz": "12",
        "kmp": "13",
        "time": 0,
        "hour": 0,
        "maps": "www.blablabla.nl",
        "created_at": null,
        "updated_at": null
      },
      {
        "id": 2,
        "user_id": 1,
        "description": "asdf",
        "from": "asdf",
        "to": "asdf",
        "kmz": "3",
        "kmp": "1",
        "time": 1,
        "hour": 0,
        "maps": "www.test.nl",
        "created_at": null,
        "updated_at": null
      }
    ]
  }
}

--EDIT--

<div class="ride-item-content">
  <select v-model="ride.location_id">
    <option v-for="location in locations" v-bind:selected="$index === 0 ? 'selected' : false">
      {{ location.from }} - {{ location.to }}
    </option>
  </select>
</div>

5 Answers 5

11

VueJS 1:

<option v-for="location in locations" v-bind:value="location.id" v-bind:selected="$index === 0 ? 'selected' : false">
  {{ location.from }} - {{ location.to }}
</option>

VueJS 2:

<option v-for="(location, index) in locations" v-bind:value="location.id" v-bind:selected="index === 0">
  {{ location.from }} - {{ location.to }}
</option>

In Vue 1, $index is automatically available inside v-for loops.

In Vue 2, you need to explicitly declare a variable for the index.

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

6 Comments

Thankyou, but it's not working? I use vueJS 1. Please see my edit.
The solution for vuejs 2 won't work if you are using v-model on the select. See first note in this docs
:selected and :value are also correct in VueJS v2 :)
@Choma in that case how do you use v-modal on the select and also default to first on the index to be selected? Not sure from the referenced link. Thanks
@raison, I guess there may be several ways to do this, but here is an easy one
|
4

In your json I cant find location.id. And your "locations" is obj. But if the "locations" is arr, you can use this:

  <select v-model="selectedLocation">
    <option v-for="location in locations" :key="location.id" :value=location.id>
      {{ location.somekey }}
    </option>
  </select>

in data -

selectedLocation: this.locations.length ? this.locations[0].id : ''

It can work because your select depends on selectedLocation (v-model).

1 Comment

you solution doesn't work it give me Cannot read property ' ..... ' of undefined, seems that data work some differently
2
created(){
    if(this.locations) {
        this.ride.location_id = this.locations[0].id
    }
}

working example jsfidlle link https://jsfiddle.net/Rashid_Bukhari/q2ro6pz1/20/

you just need to update your v-model ride.location_id into vuejs created(){} or mounted(){} method

Comments

1

You can use v-model in your select , when you load your list pass first value to a model variable and always first value will be selected

Comments

0

Similar to @J. Bruni's answer although that seemed to not work for you. Try this:

<option v-for="location in locations" v-bind:value="location.id" v-bind:selected="$index === 0 ? true : false">
    {{ location.from }} - {{ location.to }}
</option>

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.