N
N
Nikolino2018-10-10 04:31:36
Vue.js
Nikolino, 2018-10-10 04:31:36

On click show current block in v-for?

How to show only the description block whose title was clicked? When clicked, all blocks with the message-body class are shown/hidden, but only the current one needs to be hidden/showed.

<article class="message" v-for="todo in todos">

  <div class="message-header">
    <p @click="visible = !visible">{{ todo.title }}</p>
    <button class="delete" aria-label="delete" @click="removeToDo(todo)"></button>
  </div>
  
  <div class="message-body" v-if="visible">
  {{ todo.description }}
  </div>

data: {
        visible: false,
        todoTitle: '',
        todoDesc: '',
        todos: [
        {title: "Do something", description: "To do something", completed: false, id: 0},
        {title: "Do something 2", description: "To do something 2", completed: false, id: 1},
        {title: "Do something 3", description: "To do something 3", completed: false, id: 2},
        ]
    },

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mShpakov, 2018-10-10
@Nikolino

Make data like this:

data: {
        todoTitle: '',
        todoDesc: '',
        todos: [
        {isOpen:false, title: "Do something", description: "To do something", completed: false, id: 0},
        {isOpen:false, title: "Do something 2", description: "To do something 2", completed: false, id: 1},
        {isOpen:false, title: "Do something 3", description: "To do something 3", completed: false, id: 2},
        ]
    },

And in the template so v-if="todo.isOpen" and so on.
The bottom line is that you have one vizibl for all list items

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question