8

I want to add on extra property(showMore) dynamically to array of object inside v-for. is it possible to do it inside v-for like:

this.students = [
{
 name: 'anonymous',
 class: 'Sixth',
 otherInfo: "..."
},
{
 name: 'anonymous2',
 class: 'Sixth',
 otherInfo: "..."
}
]
<div v-for="student in students">
  <div>{{student.name}}</div>
  <div>{{student.class}}</div>
  <div @click="student.showMore = true">Show more +</div> 
  <!-- it was possible in angularjs. is it possible in vuejs ?-->
</div>

here i am adding one more property (showMore) to array of object(student) by clicking on Show more. is it possible inside v-for? if not then how to achieve this(with best practice)?

2
  • I looking for same thing.Have you got any solutions? Commented May 2, 2019 at 12:58
  • @SavanS Please check my answer Commented May 2, 2019 at 13:51

3 Answers 3

8

Sure, but it will not be reactive though. If you want that, you need to use:

vm.$set( target, key, value )

https://v2.vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats

Sign up to request clarification or add additional context in comments.

Comments

6

use Vue.set(obj, property, value);

Detailed example:

this.students = [
{
 name: 'anonymous',
 class: 'Sixth',
 otherInfo: "..."
},
{
 name: 'anonymous2',
 class: 'Sixth',
 otherInfo: "..."
}
]
<div v-for="student in students">
  <div>{{student.name}}</div>
  <div>{{student.class}}</div>
  <div @click="showMore(student)">Show more +</div> 
  <!-- it was possible in angularjs. is it possible in vuejs ?-->
</div>

showMore(student) {
  Vue.set(student, 'showMore', true);
}

Note: Don't forgot to import Vue

2 Comments

Correct. This works for me. Also, according to Vue's documentation, Vue.set must be used to add new properties to reactive objects. Read more here.
yes it works for me . Vue.set(object, 'key','value');
-2

If you are targeting modern browsers, you can always use the summary tag:

<details>
  <summary>More Info</summary>
  <p>hello!</p>
</details>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.