admin管理员组

文章数量:1189403

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>

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>
Share Improve this question edited Sep 23, 2016 at 20:34 Jamie asked Sep 23, 2016 at 19:37 JamieJamie 10.9k35 gold badges108 silver badges198 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 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.

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).

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

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

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>

本文标签: javascriptSelect always first value select vuejsStack Overflow