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
5 Answers
Reset to default 11VueJS 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
版权声明:本文标题:javascript - Select always first value select vue.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738411090a2085326.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论