VueJs reference selected object
I am trying to display a fullName function using selected value
HTML :
<div id="test">
<h1>{{ sayHi }}</h1>
<select v-model="selected">
<option v-for="person in persons" v-bind:value="person.about">
{{ person.lname }}
</option>
</select>
<p> {{ selected }} </p>
JS
New Vue({
el: '#test',
data: {
selected : '',
persons: [
{ fname: 'Foo' ,
lname : 'Foo2',
about : 'loren ipsum'},
{ fname: 'Bar' ,
lname:'Bar2',
about: 'dolor met'}
]
},
computed :{
sayHi : function() {
return this.fname + " " + this.lname
}
}
})
h1 should return based on selected object value,
this code returns undefined,(doesnt run at all if I pass in selected as arg) although the other parts work.Im not sure how to get the computed function to reference the selected object?
Edit:After using the great suggestions,Added a codepen for anyone else who is just starting:
https://codepen.io/2f2f/pen/rQdKKw
javascript vue.js
add a comment |
I am trying to display a fullName function using selected value
HTML :
<div id="test">
<h1>{{ sayHi }}</h1>
<select v-model="selected">
<option v-for="person in persons" v-bind:value="person.about">
{{ person.lname }}
</option>
</select>
<p> {{ selected }} </p>
JS
New Vue({
el: '#test',
data: {
selected : '',
persons: [
{ fname: 'Foo' ,
lname : 'Foo2',
about : 'loren ipsum'},
{ fname: 'Bar' ,
lname:'Bar2',
about: 'dolor met'}
]
},
computed :{
sayHi : function() {
return this.fname + " " + this.lname
}
}
})
h1 should return based on selected object value,
this code returns undefined,(doesnt run at all if I pass in selected as arg) although the other parts work.Im not sure how to get the computed function to reference the selected object?
Edit:After using the great suggestions,Added a codepen for anyone else who is just starting:
https://codepen.io/2f2f/pen/rQdKKw
javascript vue.js
add a comment |
I am trying to display a fullName function using selected value
HTML :
<div id="test">
<h1>{{ sayHi }}</h1>
<select v-model="selected">
<option v-for="person in persons" v-bind:value="person.about">
{{ person.lname }}
</option>
</select>
<p> {{ selected }} </p>
JS
New Vue({
el: '#test',
data: {
selected : '',
persons: [
{ fname: 'Foo' ,
lname : 'Foo2',
about : 'loren ipsum'},
{ fname: 'Bar' ,
lname:'Bar2',
about: 'dolor met'}
]
},
computed :{
sayHi : function() {
return this.fname + " " + this.lname
}
}
})
h1 should return based on selected object value,
this code returns undefined,(doesnt run at all if I pass in selected as arg) although the other parts work.Im not sure how to get the computed function to reference the selected object?
Edit:After using the great suggestions,Added a codepen for anyone else who is just starting:
https://codepen.io/2f2f/pen/rQdKKw
javascript vue.js
I am trying to display a fullName function using selected value
HTML :
<div id="test">
<h1>{{ sayHi }}</h1>
<select v-model="selected">
<option v-for="person in persons" v-bind:value="person.about">
{{ person.lname }}
</option>
</select>
<p> {{ selected }} </p>
JS
New Vue({
el: '#test',
data: {
selected : '',
persons: [
{ fname: 'Foo' ,
lname : 'Foo2',
about : 'loren ipsum'},
{ fname: 'Bar' ,
lname:'Bar2',
about: 'dolor met'}
]
},
computed :{
sayHi : function() {
return this.fname + " " + this.lname
}
}
})
h1 should return based on selected object value,
this code returns undefined,(doesnt run at all if I pass in selected as arg) although the other parts work.Im not sure how to get the computed function to reference the selected object?
Edit:After using the great suggestions,Added a codepen for anyone else who is just starting:
https://codepen.io/2f2f/pen/rQdKKw
javascript vue.js
javascript vue.js
edited Nov 24 '18 at 11:00
dan brown
asked Nov 24 '18 at 6:51
dan browndan brown
6817
6817
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Code returns undefined
because there is no this.fname
, nor this.lname
- it refers to data
object, which has only selected
and persons
.
You can use selected as holder for person from the loop, so in your method you can get person first and last names, but you need to modify bind value.
Try this:
new Vue({
el: '#test',
data: {
selected : '',
persons: [
{ fname: 'Foo' ,
lname : 'Foo2',
about : 'loren ipsum'},
{ fname: 'Bar' ,
lname:'Bar2',
about: 'dolor met'}
]
},
computed :{
sayHi : function() {
return this.selected.fname + " " + this.selected.lname
}
}
});
<script src="https://unpkg.com/vue"></script>
<div id="test">
<h1>{{ sayHi }}</h1>
<select v-model="selected">
<option v-for="person in persons" v-bind:value="person">
{{ person.lname }}
</option>
</select>
<p> {{ selected.about }} </p>
</div>
add a comment |
fname
and lname
are not defined in data
, so this.fname
and this.lname
are always undefined; You need to find the selected person object first based on the selected
value, and then compute the full name from the fname
and lname
.
new Vue({
el: '#test',
data: {
selected : '',
persons: [
{ fname: 'Foo' ,
lname : 'Foo2',
about : 'loren ipsum'},
{ fname: 'Bar' ,
lname:'Bar2',
about: 'dolor met'}
]
},
computed: {
sayHi: function() {
var selectedP = this.persons.find(p => p.about === this.selected)
return selectedP ? selectedP.fname + " " + selectedP.lname : ''
}
}
})
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<div id="test">
<h1>{{ sayHi }}</h1>
<select v-model="selected">
<option v-for="person in persons" v-bind:value="person.about">
{{ person.lname }}
</option>
</select>
<p> {{ selected }} </p>
</div>
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',
autoActivateHeartbeat: false,
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%2f53455878%2fvuejs-reference-selected-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Code returns undefined
because there is no this.fname
, nor this.lname
- it refers to data
object, which has only selected
and persons
.
You can use selected as holder for person from the loop, so in your method you can get person first and last names, but you need to modify bind value.
Try this:
new Vue({
el: '#test',
data: {
selected : '',
persons: [
{ fname: 'Foo' ,
lname : 'Foo2',
about : 'loren ipsum'},
{ fname: 'Bar' ,
lname:'Bar2',
about: 'dolor met'}
]
},
computed :{
sayHi : function() {
return this.selected.fname + " " + this.selected.lname
}
}
});
<script src="https://unpkg.com/vue"></script>
<div id="test">
<h1>{{ sayHi }}</h1>
<select v-model="selected">
<option v-for="person in persons" v-bind:value="person">
{{ person.lname }}
</option>
</select>
<p> {{ selected.about }} </p>
</div>
add a comment |
Code returns undefined
because there is no this.fname
, nor this.lname
- it refers to data
object, which has only selected
and persons
.
You can use selected as holder for person from the loop, so in your method you can get person first and last names, but you need to modify bind value.
Try this:
new Vue({
el: '#test',
data: {
selected : '',
persons: [
{ fname: 'Foo' ,
lname : 'Foo2',
about : 'loren ipsum'},
{ fname: 'Bar' ,
lname:'Bar2',
about: 'dolor met'}
]
},
computed :{
sayHi : function() {
return this.selected.fname + " " + this.selected.lname
}
}
});
<script src="https://unpkg.com/vue"></script>
<div id="test">
<h1>{{ sayHi }}</h1>
<select v-model="selected">
<option v-for="person in persons" v-bind:value="person">
{{ person.lname }}
</option>
</select>
<p> {{ selected.about }} </p>
</div>
add a comment |
Code returns undefined
because there is no this.fname
, nor this.lname
- it refers to data
object, which has only selected
and persons
.
You can use selected as holder for person from the loop, so in your method you can get person first and last names, but you need to modify bind value.
Try this:
new Vue({
el: '#test',
data: {
selected : '',
persons: [
{ fname: 'Foo' ,
lname : 'Foo2',
about : 'loren ipsum'},
{ fname: 'Bar' ,
lname:'Bar2',
about: 'dolor met'}
]
},
computed :{
sayHi : function() {
return this.selected.fname + " " + this.selected.lname
}
}
});
<script src="https://unpkg.com/vue"></script>
<div id="test">
<h1>{{ sayHi }}</h1>
<select v-model="selected">
<option v-for="person in persons" v-bind:value="person">
{{ person.lname }}
</option>
</select>
<p> {{ selected.about }} </p>
</div>
Code returns undefined
because there is no this.fname
, nor this.lname
- it refers to data
object, which has only selected
and persons
.
You can use selected as holder for person from the loop, so in your method you can get person first and last names, but you need to modify bind value.
Try this:
new Vue({
el: '#test',
data: {
selected : '',
persons: [
{ fname: 'Foo' ,
lname : 'Foo2',
about : 'loren ipsum'},
{ fname: 'Bar' ,
lname:'Bar2',
about: 'dolor met'}
]
},
computed :{
sayHi : function() {
return this.selected.fname + " " + this.selected.lname
}
}
});
<script src="https://unpkg.com/vue"></script>
<div id="test">
<h1>{{ sayHi }}</h1>
<select v-model="selected">
<option v-for="person in persons" v-bind:value="person">
{{ person.lname }}
</option>
</select>
<p> {{ selected.about }} </p>
</div>
new Vue({
el: '#test',
data: {
selected : '',
persons: [
{ fname: 'Foo' ,
lname : 'Foo2',
about : 'loren ipsum'},
{ fname: 'Bar' ,
lname:'Bar2',
about: 'dolor met'}
]
},
computed :{
sayHi : function() {
return this.selected.fname + " " + this.selected.lname
}
}
});
<script src="https://unpkg.com/vue"></script>
<div id="test">
<h1>{{ sayHi }}</h1>
<select v-model="selected">
<option v-for="person in persons" v-bind:value="person">
{{ person.lname }}
</option>
</select>
<p> {{ selected.about }} </p>
</div>
new Vue({
el: '#test',
data: {
selected : '',
persons: [
{ fname: 'Foo' ,
lname : 'Foo2',
about : 'loren ipsum'},
{ fname: 'Bar' ,
lname:'Bar2',
about: 'dolor met'}
]
},
computed :{
sayHi : function() {
return this.selected.fname + " " + this.selected.lname
}
}
});
<script src="https://unpkg.com/vue"></script>
<div id="test">
<h1>{{ sayHi }}</h1>
<select v-model="selected">
<option v-for="person in persons" v-bind:value="person">
{{ person.lname }}
</option>
</select>
<p> {{ selected.about }} </p>
</div>
answered Nov 24 '18 at 7:09
GasanovGasanov
34328
34328
add a comment |
add a comment |
fname
and lname
are not defined in data
, so this.fname
and this.lname
are always undefined; You need to find the selected person object first based on the selected
value, and then compute the full name from the fname
and lname
.
new Vue({
el: '#test',
data: {
selected : '',
persons: [
{ fname: 'Foo' ,
lname : 'Foo2',
about : 'loren ipsum'},
{ fname: 'Bar' ,
lname:'Bar2',
about: 'dolor met'}
]
},
computed: {
sayHi: function() {
var selectedP = this.persons.find(p => p.about === this.selected)
return selectedP ? selectedP.fname + " " + selectedP.lname : ''
}
}
})
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<div id="test">
<h1>{{ sayHi }}</h1>
<select v-model="selected">
<option v-for="person in persons" v-bind:value="person.about">
{{ person.lname }}
</option>
</select>
<p> {{ selected }} </p>
</div>
add a comment |
fname
and lname
are not defined in data
, so this.fname
and this.lname
are always undefined; You need to find the selected person object first based on the selected
value, and then compute the full name from the fname
and lname
.
new Vue({
el: '#test',
data: {
selected : '',
persons: [
{ fname: 'Foo' ,
lname : 'Foo2',
about : 'loren ipsum'},
{ fname: 'Bar' ,
lname:'Bar2',
about: 'dolor met'}
]
},
computed: {
sayHi: function() {
var selectedP = this.persons.find(p => p.about === this.selected)
return selectedP ? selectedP.fname + " " + selectedP.lname : ''
}
}
})
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<div id="test">
<h1>{{ sayHi }}</h1>
<select v-model="selected">
<option v-for="person in persons" v-bind:value="person.about">
{{ person.lname }}
</option>
</select>
<p> {{ selected }} </p>
</div>
add a comment |
fname
and lname
are not defined in data
, so this.fname
and this.lname
are always undefined; You need to find the selected person object first based on the selected
value, and then compute the full name from the fname
and lname
.
new Vue({
el: '#test',
data: {
selected : '',
persons: [
{ fname: 'Foo' ,
lname : 'Foo2',
about : 'loren ipsum'},
{ fname: 'Bar' ,
lname:'Bar2',
about: 'dolor met'}
]
},
computed: {
sayHi: function() {
var selectedP = this.persons.find(p => p.about === this.selected)
return selectedP ? selectedP.fname + " " + selectedP.lname : ''
}
}
})
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<div id="test">
<h1>{{ sayHi }}</h1>
<select v-model="selected">
<option v-for="person in persons" v-bind:value="person.about">
{{ person.lname }}
</option>
</select>
<p> {{ selected }} </p>
</div>
fname
and lname
are not defined in data
, so this.fname
and this.lname
are always undefined; You need to find the selected person object first based on the selected
value, and then compute the full name from the fname
and lname
.
new Vue({
el: '#test',
data: {
selected : '',
persons: [
{ fname: 'Foo' ,
lname : 'Foo2',
about : 'loren ipsum'},
{ fname: 'Bar' ,
lname:'Bar2',
about: 'dolor met'}
]
},
computed: {
sayHi: function() {
var selectedP = this.persons.find(p => p.about === this.selected)
return selectedP ? selectedP.fname + " " + selectedP.lname : ''
}
}
})
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<div id="test">
<h1>{{ sayHi }}</h1>
<select v-model="selected">
<option v-for="person in persons" v-bind:value="person.about">
{{ person.lname }}
</option>
</select>
<p> {{ selected }} </p>
</div>
new Vue({
el: '#test',
data: {
selected : '',
persons: [
{ fname: 'Foo' ,
lname : 'Foo2',
about : 'loren ipsum'},
{ fname: 'Bar' ,
lname:'Bar2',
about: 'dolor met'}
]
},
computed: {
sayHi: function() {
var selectedP = this.persons.find(p => p.about === this.selected)
return selectedP ? selectedP.fname + " " + selectedP.lname : ''
}
}
})
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<div id="test">
<h1>{{ sayHi }}</h1>
<select v-model="selected">
<option v-for="person in persons" v-bind:value="person.about">
{{ person.lname }}
</option>
</select>
<p> {{ selected }} </p>
</div>
new Vue({
el: '#test',
data: {
selected : '',
persons: [
{ fname: 'Foo' ,
lname : 'Foo2',
about : 'loren ipsum'},
{ fname: 'Bar' ,
lname:'Bar2',
about: 'dolor met'}
]
},
computed: {
sayHi: function() {
var selectedP = this.persons.find(p => p.about === this.selected)
return selectedP ? selectedP.fname + " " + selectedP.lname : ''
}
}
})
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
<div id="test">
<h1>{{ sayHi }}</h1>
<select v-model="selected">
<option v-for="person in persons" v-bind:value="person.about">
{{ person.lname }}
</option>
</select>
<p> {{ selected }} </p>
</div>
answered Nov 24 '18 at 7:08
PsidomPsidom
126k1290134
126k1290134
add a comment |
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.
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%2f53455878%2fvuejs-reference-selected-object%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