A
A
Alexey Bespalov2019-04-02 10:51:04
Vue.js
Alexey Bespalov, 2019-04-02 10:51:04

How to close all components (folders) when clicking on any of them or on a nested element?

The application looks like a list of folders with items inside. Clicking on a folder opens a list of items.
Both the folder and the elements are separate components for me.
And it turns out that when you click on any element or folder, other folders do not know about it. And it turns out that several folders are open and until you click them all, they will not close. Uncomfortable.
But I can't figure out how to tell all folder components that there was a click and it's time to close them.
Some code for understanding:

//Контейнер который содержит папки
    <div v-for="currentBk of bkFolder"
        :key="currentBk.id"
        >
        <BkFolder v-if="currentBk.children"
          :bkChildren = currentBk.children
          :title = currentBk.title 
          :index = currentBk.index 
        >
        </BkFolder>

        <BkItem v-else
          :currentBk = currentBk
        >
        </BkItem> 
    </div>
//*************** Вывод папок
<template>
<div>

  <div v-if="index < 4"
    v-on:click="setOpenFolder"
    class="bk-folder">
    {{ title }}
  </div>

  <BkOpenFolder 
    v-if="openFolder"
    v-on:click="setOpenFolder"
    :bkChildren = bkChildren
  />

  </div>
</template>

//********** Раскрытая папка с вложенными элементами
<template>
<div
  v-if="openFolder"
  v-on:click="setCloseFolder"
  >
    <div class="title">
      {{ title }}
    </div>
  <div 
    class="bk-open-folder"
    
  >
    <div v-for="currentBk of bkChildren"
        :key="currentBk.id"
        >
        <BkFolder v-if="currentBk.children"
          :bkFolder = currentBk.children
          :title = currentBk.title 
        >
        </BkFolder>

        <BkItem v-else
          :currentBk = currentBk
        >
        </BkItem> 
    </div>
  </div>
</div>
</template>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Klein Maximus, 2019-04-02
@kleinmaximus

Any click outside the component is caught and some kind of handler is launched.
https://github.com/ndelvalle/v-click-outside
https://github.com/nchutchind/vue-outside-events

0
0xD34F, 2019-04-02
@0xD34F Vue.js

Add a property to the folder component that will indicate which of the child elements is open, and let the value of the parameter that controls the openness of the child elements depend on the value of this property. For example .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question