0

So I'm looping through my allItems Array of Objects and I want to show something when a certain button is clicked.

allItems

allItems = [
   {
       title: 'test',
       show: false,
       description: 'this is test'
   },
   {
       title: 'test 1',
       show: false,
       description: 'the test 1'
   }
]

List.vue

v-col cols="3" v-for="item in allItems" :key="item.id">
      <v-card width="100%">

        <v-card-text>
          <span class="text--primary">
            <span class="title">{{ item.title.substring(0,28)+".." }}</span>
          </span>
        </v-card-text>

        <v-card-actions>
          <v-btn icon @click="item.show = !item.show">
            <v-icon>{{ item.show ? 'mdi-menu-up' : 'mdi-menu-down' }}</v-icon>
          </v-btn>
        </v-card-actions>
        <v-expand-transition>
          <div v-if="item.show">
            <span class="pt-10">{{ item.description }}</span>
          </div>
        </v-expand-transition>
      </v-card>
 </v-col>

My problem is when the button is getting clicked its not opening the description.

EDIT

It looks like I didn't put much info. I'm using Vuex to handle my state.

PS: I'm not passing the computed value anymore to the child component.

in my List.vue script

import { mapActions, mapGetters } from "vuex";

export default {
data: () => ({}),
methods: {
  ...mapActions(["fetchItems"])
},
created() {
  this.fetchItems();
},
computed: mapGetters(["allItems"])
};
4
  • The code you posted is working fine when i reproduced. Where did you define allItems ? Is is inside component's data method? Commented Aug 26, 2019 at 15:36
  • @NafeesAnwar fromprops: ['allItems'] Commented Aug 26, 2019 at 15:38
  • @Priz I'd suggest checking where you are setting the props, as this code works perfect: codepen.io/Tiarhai/pen/QWLpzvQ Commented Aug 26, 2019 at 15:42
  • You must add to element v:bind attr . vuejs.org/v2/guide/components.html Commented Aug 26, 2019 at 15:43

2 Answers 2

2

From your reply to the comments, I see you are passing your data as a prop and want it to be reactive. In Vue, all props form a one-way-down binding between the child property and the parent one.

So to reactively change the prop value, you can:

  1. Set a data value which you will assign the props value to so that you can reactively change the state data instead.

  2. You can aslo define a computed value from the prop and use the computed value instead

Read more on the vue docs

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

Comments

0

As Onwuka wrote, don't modify the props directly in the child. I think what would make a lot of sense here is if you removed the show prop entirely from the items. Then you create a custom component e.g. ListItem.vue that contains your entire element and takes the item as a prop. Then each of these smaller components can keep track of its own state (visible/hidden) in the components data.

1 Comment

The same remains true for vuex state. If you want to modify the state, you would need to commit these changes to the store by using this.$store.commit() to commit a mutation. Don't directly modify the state in your component.

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.