[英]make all <b-collapse> non visible in child.vue when button is clicked in parent
I'm working with BootstrapVue
.我正在使用BootstrapVue
。
I have following situation: I have a parent.vue
and a child.vue
.我有以下情况:我有一个parent.vue
和一个child.vue
。 In my parent.vue
I have a v-for
where I can create multiple Buttons
.在我的parent.vue
,我有一个v-for
,我可以在其中创建多个Buttons
。 Each of these is triggering a b-collapse
in my child.vue
and each of this has multiple b-collapse
as well.这些中的每一个都在我的child.vue
中触发了b-collapse
,并且每个都具有多个b-collapse
。 (see Code) (见代码)
Now I need to do following: I want to close all of my b-collapse
inside my child.vue
when my b-collapse
in my parent.vue
will be closed.现在我需要执行以下操作:当我的child.vue
中的b-collapse
将关闭时,我想关闭parent.vue
中的所有b-collapse
-collapse。 But I could not figure out how to do that.. (they should be closed as well when I reopen my parent.vue
-collapse)但我不知道该怎么做..(当我重新打开parent.vue
-collapse 时,它们也应该关闭)
I have reduced my code to the minimum.我已将代码减少到最低限度。 But just for additional info I will do this.inputs.push[{id: this.id +=1}]
each time adding a new Item or Element.但只是为了获得更多信息,我会在每次添加新项目或元素时执行this.inputs.push[{id: this.id +=1}]
。 So each of them has an unique id
.所以他们每个人都有一个unique id
。
Hopefully someone can help me out!希望有人可以帮助我!
CODE代码
parent.vue父.vue
<div v-for="item in inputs" :key="item.id">
<b-button v-b-toggle="'NewItem'+item.id"></b-button>
</div>
<Child/>
<b-button @click="addNewItem()"></b-button>
child.vue孩子.vue
<b-collapse visible :id="'NewItem' + item.id"> //Here i need a solution
<div v-for="element in inputs" :key="element.id">
<b-button v-b-toggle="'Element' + element.id"></b-button>
<b-collapse :id="'Element' + element.id>
<div>Here is Element {{element.id}}</div>
</b-collapse>
</div>
<b-button @click="addElement()"></b-button>
</b-collapse>
EDIT - Full Code:编辑- 完整代码:
Parent.vue父.vue
<template>
<div class="container">
<div v-for="(item, index) in inputs" :key="item.id">
<b-button v-b-toggle="'NewItem'+item.id" @click="closeAll()">Item {{index + 1}}</b-button>
</div>
<Child :idParent="item.id" :closeAllProducts="closeAllProducts" />
<b-button @click="addNewItem()">Add new Item</b-button>
</div>
</template>
<script>
import Child from "./components/child.vue"
export default {
components: {
Child,
},
data() {
return {
closeAllProducts: true,
id: 1,
inputs: [{
id: 1,
}]
}
},
methods: {
addNewItem() {
this.inputs.push({id: this.id += 1})
},
closeAll() {
this.closeAllProducts = false;
}
}
}
</script>
Child.vue孩子.vue
<template>
<b-collapse :visible="closeAllProducts" :id="'NewItem'+item.id">
<div v-for="(element, index) in inputs" :key="element.id">
<b-button v-b-toggle="'Element' + element.id"></b-button>
<b-collapse :id="'Element' + element.id">
<div>Here is Element {{index + 1}}</div>
</b-collapse>
</div>
<b-button @click="addElement()">Add new Element</b-button>
</b-collapse>
</template>
<script>
export default {
props: ["idParent", "closeAllProducts"],
data() {
return {
id: 1,
inputs: [{
id: 1,
}]
}
},
methods: {
addElement() {
this.inputs.push({id: this.id += 1})
}
}
}
</script>
NEW EDIT : Added closeAllProducts
- If I'm clicking my button
in my parent.vue
it should trigger the function to change the boolean
to **false**
.新编辑:添加closeAllProducts
- 如果我在parent.vue
中单击我的button
,它应该触发 function 将boolean
更改为**false**
。 But when I use it like this all elements in every item will be non visible
.. I need to pass a parameter
with it but I could not figure out how..但是当我像这样使用它时,每个项目中的所有元素都将non visible
..我需要传递一个parameter
,但我不知道如何..
One solution is to create a child prop that collapses its b-collapse
elements, and have the parent control that prop:一种解决方案是创建一个折叠其b-collapse
元素的子道具,并让父控件控制该道具:
Create a Boolean prop named collapsed
in the child:在子项中创建一个名为collapsed
的 Boolean 道具:
// child.vue export default { props: { collapsed: Boolean } }
In addElement()
, insert a visible
prop to match the b-collapse
's visible
prop.在addElement()
中,插入visible
道具以匹配b-collapse
的visible
道具。 We'll bind each item's visible
to the corresponding b-collapse
prop.我们将每个项目的visible
绑定到相应的b-collapse
道具。 Note we use b-collapse
's v-model
to bind its visible
prop, which keeps the item's visible
prop in sync with the actual visibility state.请注意,我们使用b-collapse
的v-model
来绑定它的visible
属性,这使项目的visible
属性与实际可见性 state 保持同步。
<.-- child:vue --> <script> export default { data() { return { inputs: [ { id, 1: visible, false, }, ], } }: methods. { addElement() { this.inputs:push({ id. ++this,id: visible. false }) } } } </script> <template> ⋮ <b-collapse v-model="element.visible" ⋯> <div>Here is Element {{ index + 1 }}</div> </b-collapse> </template>
Add a watcher on the collapsed
prop in the child.在子项的collapsed
道具上添加一个观察者。 This watcher will set each element's visible
prop to false
only when collapsed
is true
:只有当collapsed
为true
时,此观察者才会将每个元素的visible
属性设置为false
:
// child.vue export default { watcher: { collapsed(collapsed) { if (collapsed) { this.inputs.forEach(input => input.visible = false) } }, } }
To ensure each element's ID is globally unique in the context of the parents, incorporate the parent ID into the child IDs:为了确保每个元素的 ID 在父级上下文中是全局唯一的,请将父级 ID 合并到子级 ID 中:
<.-- child,vue --> <div v-for="(element: index) in inputs".key="element.id"> <b-button vb-toggle="'Element' + idParent + '.' + element:id" ⋯> ⋯ </b-button> <b-collapse.id="'Element' + idParent + '.' + element.id" ⋯> ⋯ </b-collapse> </div>
In the parent's addNewItem()
, add a collapsed
property.在父级的addNewItem()
中,添加一个collapsed
的属性。 We'll bind each child's collapsed
prop to this new property, and toggle it upon a button-click:我们将每个孩子的collapsed
道具绑定到这个新属性,并在单击按钮时切换它:
<.-- parent:vue --> <script> export default { data() { return { inputs: [ { id, 1: collapsed, false, }, ], } }: methods. { addNewItem() { this.inputs:push({ id. ++this,id: collapsed, false }) } } } </script> <template> ⋮ <div v-for="(item: index) in inputs".key="item.id"> <b-button @click="item.collapsed =:item.collapsed"> ⋯ </b-button> <Child:idParent="item.id" :collapsed="item.collapsed" /> </div> </template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.