1

I'm struggling to find out on how to render the selected values in Vue.

I have a form that runs a query based on the selection:

<form id="Register">   
    <br>Firstname<input type="checkbox" value="firstName">
    <br>Lastname<input type="checkbox" value="lastName">
    <br>Nickname<input type="checkbox" value="nickName">
    <br>Mobile<input type="checkbox" value="Mobile"">
    <br><button type="button" id="submit">Submit</button>
</form>

Then the queries return a JSON object like this:

data: {
    names: [
        { firstName: "Jessica", lastName: "Jones" },
        { firstName: "Mike", lastName: "Lebowski" }
    ]
}

But when i try to render this in Vue using v-for

<div class v-for="name in names">
    {{name}}
</div>

-or-

<div class v-for="name in names">
    <div class v-for="details in name"> 
        {{details}}
    </div>
</div>

It renders either with the entire object

{ firstName: "Jessica", lastname: "Jones" }

or with firstname on one row and lastname on another.

Is there some way you can render everything in one row in the HTML based on the selected input?

2 Answers 2

2

You can directly access a property in an object with objectName.propertyName.

In your case:

<div class v-for="name in names">
    {{name.firstName}} {{name.lastName}}
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

Yes I know, but since the property name Will not always be accsesible it doesn’t work. Since I get diffrent data anses on the selection
You could wrap them in v-ifthen: <p v-if="name.firstName">{{ name.firstName }}</p> <p v-if="name.lastName"> {{ name.lastName }}</p>
That would require alot of v-if statements... Since there will be multiple selections in the final code
1

You can use destructuring also:

<div class v-for="({firstName, lastName}) in names">
  {{firstName}} {{lastName}}
</div>

Or, map the properties in line:

<div class v-for="name in names">
  {{ Object.keys(name).map(key => name[key]).join(' ') }}
</div>

4 Comments

But if I choose mobile as well as firstname and lastname the data will return with 3 keys.But the data binding only renders 2: {{firstName}} {{lastName}}
So add mobile also. You're not restricted to only two properties.
I think I didn't explain myself good enough. If I make the selection och firstName and lastName and run the query with {{firstName}} {{lastName}} {{mobile}} I will get an error that {{mobile}} is empty. Since the query will only return values that you select the data bindnings can't be fixed. Do you understand?
Didn't see your edit! That is the answer! Thank you so much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.