Populating options for select using v-for VueJS after handling load data event
up vote
2
down vote
favorite
I have this VueJS application written using Typescript. I'm using Axios to get data from my database. This works well and the result I get is an array which is what I was expecting. When I do console.log
on this array I can see that it's the correct result.
However, when I try to loop through this array to create the options for my select drop down I get a blank list. Why isn't the result coming up even though the variable I'm looping through is an array?
Edit: I've added a picture here showing the Axios result (response.data
)
export default class EditRoute extends Vue {
result: any;
selectedRoute: string;
constructor(){
super();
this.selectedRoute = "";
this.result = ;
}
loadData() {
axios.get('http://localhost:8080/routes')
.then(response => (this.result = response.data));
}
}
<select name="routeSelect" v-model="selectedRoute">
<option v-for="routes in result" v-bind:key="routes.name" v-bind:value="routes.name"> {{ routes.name }} </option>
</select>
<button @click="loadData">Load data</button>
html node.js typescript vue.js axios
add a comment |
up vote
2
down vote
favorite
I have this VueJS application written using Typescript. I'm using Axios to get data from my database. This works well and the result I get is an array which is what I was expecting. When I do console.log
on this array I can see that it's the correct result.
However, when I try to loop through this array to create the options for my select drop down I get a blank list. Why isn't the result coming up even though the variable I'm looping through is an array?
Edit: I've added a picture here showing the Axios result (response.data
)
export default class EditRoute extends Vue {
result: any;
selectedRoute: string;
constructor(){
super();
this.selectedRoute = "";
this.result = ;
}
loadData() {
axios.get('http://localhost:8080/routes')
.then(response => (this.result = response.data));
}
}
<select name="routeSelect" v-model="selectedRoute">
<option v-for="routes in result" v-bind:key="routes.name" v-bind:value="routes.name"> {{ routes.name }} </option>
</select>
<button @click="loadData">Load data</button>
html node.js typescript vue.js axios
1
i'm trying an example like yours and it works fine
– Boussadjra Brahim
Nov 15 at 20:18
@BoussadjraBrahim Okay. I'll post a picture of the result I get from my Axios call maybe that's the problem then
– M. H
Nov 15 at 20:20
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have this VueJS application written using Typescript. I'm using Axios to get data from my database. This works well and the result I get is an array which is what I was expecting. When I do console.log
on this array I can see that it's the correct result.
However, when I try to loop through this array to create the options for my select drop down I get a blank list. Why isn't the result coming up even though the variable I'm looping through is an array?
Edit: I've added a picture here showing the Axios result (response.data
)
export default class EditRoute extends Vue {
result: any;
selectedRoute: string;
constructor(){
super();
this.selectedRoute = "";
this.result = ;
}
loadData() {
axios.get('http://localhost:8080/routes')
.then(response => (this.result = response.data));
}
}
<select name="routeSelect" v-model="selectedRoute">
<option v-for="routes in result" v-bind:key="routes.name" v-bind:value="routes.name"> {{ routes.name }} </option>
</select>
<button @click="loadData">Load data</button>
html node.js typescript vue.js axios
I have this VueJS application written using Typescript. I'm using Axios to get data from my database. This works well and the result I get is an array which is what I was expecting. When I do console.log
on this array I can see that it's the correct result.
However, when I try to loop through this array to create the options for my select drop down I get a blank list. Why isn't the result coming up even though the variable I'm looping through is an array?
Edit: I've added a picture here showing the Axios result (response.data
)
export default class EditRoute extends Vue {
result: any;
selectedRoute: string;
constructor(){
super();
this.selectedRoute = "";
this.result = ;
}
loadData() {
axios.get('http://localhost:8080/routes')
.then(response => (this.result = response.data));
}
}
<select name="routeSelect" v-model="selectedRoute">
<option v-for="routes in result" v-bind:key="routes.name" v-bind:value="routes.name"> {{ routes.name }} </option>
</select>
<button @click="loadData">Load data</button>
html node.js typescript vue.js axios
html node.js typescript vue.js axios
edited Nov 15 at 20:34
Boussadjra Brahim
4,5393629
4,5393629
asked Nov 15 at 20:09
M. H
927
927
1
i'm trying an example like yours and it works fine
– Boussadjra Brahim
Nov 15 at 20:18
@BoussadjraBrahim Okay. I'll post a picture of the result I get from my Axios call maybe that's the problem then
– M. H
Nov 15 at 20:20
add a comment |
1
i'm trying an example like yours and it works fine
– Boussadjra Brahim
Nov 15 at 20:18
@BoussadjraBrahim Okay. I'll post a picture of the result I get from my Axios call maybe that's the problem then
– M. H
Nov 15 at 20:20
1
1
i'm trying an example like yours and it works fine
– Boussadjra Brahim
Nov 15 at 20:18
i'm trying an example like yours and it works fine
– Boussadjra Brahim
Nov 15 at 20:18
@BoussadjraBrahim Okay. I'll post a picture of the result I get from my Axios call maybe that's the problem then
– M. H
Nov 15 at 20:20
@BoussadjraBrahim Okay. I'll post a picture of the result I get from my Axios call maybe that's the problem then
– M. H
Nov 15 at 20:20
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Since your result
is an object which is containing one item, that item is an array called routes
in this case you should loop through result.routes
like so :
<option v-for="routes in result.routes" v-bind:key="routes.name" v-bind:value="routes.name"> {{ routes.name }} </option>
Extra Example :
new Vue({
el: '#app',
data: function() {
return {
selectedUser: '',
users: ,
}
},
mounted: function() {
// this.loadData();
// this.timer = setInterval(this.loadData, 4000);
},
methods: {
loadData: function() {
axios.get('https://jsonplaceholder.typicode.com/users').then(response => {
this.users = response.data;
}).catch(e => {
})
}
}
})
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<script src="https://unpkg.com/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/vue-axios@2.1.4/dist/vue-axios.min.js"></script>
</head>
<body>
<div id="app">
<select name="userSelect" v-model="selectedUser">
<option v-for="user in users" v-bind:key="user.id" v-bind:value="user.name"> {{ user.name }} </option>
</select>
<button @click="loadData">Load data</button>
</div>
</body>
</html>
1
I tried it now it works. It makes sense too, I didn't think that it was an object and not an array. Thank you so much!
– M. H
Nov 15 at 20:29
1
you're welcome i added also a working example for futur askers
– Boussadjra Brahim
Nov 15 at 20:30
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327188%2fpopulating-options-for-select-using-v-for-vuejs-after-handling-load-data-event%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Since your result
is an object which is containing one item, that item is an array called routes
in this case you should loop through result.routes
like so :
<option v-for="routes in result.routes" v-bind:key="routes.name" v-bind:value="routes.name"> {{ routes.name }} </option>
Extra Example :
new Vue({
el: '#app',
data: function() {
return {
selectedUser: '',
users: ,
}
},
mounted: function() {
// this.loadData();
// this.timer = setInterval(this.loadData, 4000);
},
methods: {
loadData: function() {
axios.get('https://jsonplaceholder.typicode.com/users').then(response => {
this.users = response.data;
}).catch(e => {
})
}
}
})
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<script src="https://unpkg.com/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/vue-axios@2.1.4/dist/vue-axios.min.js"></script>
</head>
<body>
<div id="app">
<select name="userSelect" v-model="selectedUser">
<option v-for="user in users" v-bind:key="user.id" v-bind:value="user.name"> {{ user.name }} </option>
</select>
<button @click="loadData">Load data</button>
</div>
</body>
</html>
1
I tried it now it works. It makes sense too, I didn't think that it was an object and not an array. Thank you so much!
– M. H
Nov 15 at 20:29
1
you're welcome i added also a working example for futur askers
– Boussadjra Brahim
Nov 15 at 20:30
add a comment |
up vote
2
down vote
accepted
Since your result
is an object which is containing one item, that item is an array called routes
in this case you should loop through result.routes
like so :
<option v-for="routes in result.routes" v-bind:key="routes.name" v-bind:value="routes.name"> {{ routes.name }} </option>
Extra Example :
new Vue({
el: '#app',
data: function() {
return {
selectedUser: '',
users: ,
}
},
mounted: function() {
// this.loadData();
// this.timer = setInterval(this.loadData, 4000);
},
methods: {
loadData: function() {
axios.get('https://jsonplaceholder.typicode.com/users').then(response => {
this.users = response.data;
}).catch(e => {
})
}
}
})
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<script src="https://unpkg.com/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/vue-axios@2.1.4/dist/vue-axios.min.js"></script>
</head>
<body>
<div id="app">
<select name="userSelect" v-model="selectedUser">
<option v-for="user in users" v-bind:key="user.id" v-bind:value="user.name"> {{ user.name }} </option>
</select>
<button @click="loadData">Load data</button>
</div>
</body>
</html>
1
I tried it now it works. It makes sense too, I didn't think that it was an object and not an array. Thank you so much!
– M. H
Nov 15 at 20:29
1
you're welcome i added also a working example for futur askers
– Boussadjra Brahim
Nov 15 at 20:30
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Since your result
is an object which is containing one item, that item is an array called routes
in this case you should loop through result.routes
like so :
<option v-for="routes in result.routes" v-bind:key="routes.name" v-bind:value="routes.name"> {{ routes.name }} </option>
Extra Example :
new Vue({
el: '#app',
data: function() {
return {
selectedUser: '',
users: ,
}
},
mounted: function() {
// this.loadData();
// this.timer = setInterval(this.loadData, 4000);
},
methods: {
loadData: function() {
axios.get('https://jsonplaceholder.typicode.com/users').then(response => {
this.users = response.data;
}).catch(e => {
})
}
}
})
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<script src="https://unpkg.com/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/vue-axios@2.1.4/dist/vue-axios.min.js"></script>
</head>
<body>
<div id="app">
<select name="userSelect" v-model="selectedUser">
<option v-for="user in users" v-bind:key="user.id" v-bind:value="user.name"> {{ user.name }} </option>
</select>
<button @click="loadData">Load data</button>
</div>
</body>
</html>
Since your result
is an object which is containing one item, that item is an array called routes
in this case you should loop through result.routes
like so :
<option v-for="routes in result.routes" v-bind:key="routes.name" v-bind:value="routes.name"> {{ routes.name }} </option>
Extra Example :
new Vue({
el: '#app',
data: function() {
return {
selectedUser: '',
users: ,
}
},
mounted: function() {
// this.loadData();
// this.timer = setInterval(this.loadData, 4000);
},
methods: {
loadData: function() {
axios.get('https://jsonplaceholder.typicode.com/users').then(response => {
this.users = response.data;
}).catch(e => {
})
}
}
})
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<script src="https://unpkg.com/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/vue-axios@2.1.4/dist/vue-axios.min.js"></script>
</head>
<body>
<div id="app">
<select name="userSelect" v-model="selectedUser">
<option v-for="user in users" v-bind:key="user.id" v-bind:value="user.name"> {{ user.name }} </option>
</select>
<button @click="loadData">Load data</button>
</div>
</body>
</html>
new Vue({
el: '#app',
data: function() {
return {
selectedUser: '',
users: ,
}
},
mounted: function() {
// this.loadData();
// this.timer = setInterval(this.loadData, 4000);
},
methods: {
loadData: function() {
axios.get('https://jsonplaceholder.typicode.com/users').then(response => {
this.users = response.data;
}).catch(e => {
})
}
}
})
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<script src="https://unpkg.com/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/vue-axios@2.1.4/dist/vue-axios.min.js"></script>
</head>
<body>
<div id="app">
<select name="userSelect" v-model="selectedUser">
<option v-for="user in users" v-bind:key="user.id" v-bind:value="user.name"> {{ user.name }} </option>
</select>
<button @click="loadData">Load data</button>
</div>
</body>
</html>
new Vue({
el: '#app',
data: function() {
return {
selectedUser: '',
users: ,
}
},
mounted: function() {
// this.loadData();
// this.timer = setInterval(this.loadData, 4000);
},
methods: {
loadData: function() {
axios.get('https://jsonplaceholder.typicode.com/users').then(response => {
this.users = response.data;
}).catch(e => {
})
}
}
})
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<script src="https://unpkg.com/vue@2.5.17/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/vue-axios@2.1.4/dist/vue-axios.min.js"></script>
</head>
<body>
<div id="app">
<select name="userSelect" v-model="selectedUser">
<option v-for="user in users" v-bind:key="user.id" v-bind:value="user.name"> {{ user.name }} </option>
</select>
<button @click="loadData">Load data</button>
</div>
</body>
</html>
answered Nov 15 at 20:25
Boussadjra Brahim
4,5393629
4,5393629
1
I tried it now it works. It makes sense too, I didn't think that it was an object and not an array. Thank you so much!
– M. H
Nov 15 at 20:29
1
you're welcome i added also a working example for futur askers
– Boussadjra Brahim
Nov 15 at 20:30
add a comment |
1
I tried it now it works. It makes sense too, I didn't think that it was an object and not an array. Thank you so much!
– M. H
Nov 15 at 20:29
1
you're welcome i added also a working example for futur askers
– Boussadjra Brahim
Nov 15 at 20:30
1
1
I tried it now it works. It makes sense too, I didn't think that it was an object and not an array. Thank you so much!
– M. H
Nov 15 at 20:29
I tried it now it works. It makes sense too, I didn't think that it was an object and not an array. Thank you so much!
– M. H
Nov 15 at 20:29
1
1
you're welcome i added also a working example for futur askers
– Boussadjra Brahim
Nov 15 at 20:30
you're welcome i added also a working example for futur askers
– Boussadjra Brahim
Nov 15 at 20:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327188%2fpopulating-options-for-select-using-v-for-vuejs-after-handling-load-data-event%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
i'm trying an example like yours and it works fine
– Boussadjra Brahim
Nov 15 at 20:18
@BoussadjraBrahim Okay. I'll post a picture of the result I get from my Axios call maybe that's the problem then
– M. H
Nov 15 at 20:20