V
V
Vlad2020-06-10 15:59:56
Vue.js
Vlad, 2020-06-10 15:59:56

How to make a class toggle in Vue, but with lists expanded by default?

Hello. Here I made an example.
https://jsfiddle.net/tcv64y9k/
I don’t understand how to make the lists open by default, and already hidden on click.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2020-06-10
@Vlad024

Add the block data into an array, add a property to each array element that will be responsible for displaying content:

data: () => ({
  items: [
    { show: true, ... },
    { show: true, ... },
    ...
  ],
}),

<template v-for="(n, i) in items">
  <button @click="n.show = !n.show">{{ n.show ? 'hide' : 'open' }}</button>
  <div class="list" v-show="n.show">
    ...
  </div>
  <hr v-if="i !== items.length - 1">
</template>

https://jsfiddle.net/ra5byco9/UPD
. Taken from the comments:
With v-for, I know how, but without it, in any way?

It's not about v-for, but about the fact that the state of each hidden-showed element should be managed by a separate property. You can make it, a property, part of the component responsible for demonstrating the button-content pair. Then you can create as many instances as you need .

A
Andrey, 2020-06-11
@svistiboshka

You are looking in the wrong direction and trying to work with the house, well, if so, then here you are
https://jsfiddle.net/za3o5j9m/1/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question