[英]How can I show and hide a div based on what's clicked in another div in Vue js?
I have 2 divs, one that shows the names of the products(Product 1, Product 2... etc) and the other that shows the product articles(Product 1: Article 1, Product 2: Article 1, Product 2: Article 2... ).我有 2 个 div,一个显示产品名称(产品 1、产品 2...等),另一个显示产品文章(产品 1:第 1 条、产品 2:第 1 条、产品 2:第 2 条) ……)。 I would like to have a functionality where I click Product 1 and then Product 1's articles should appear.
我想要一个功能,我点击产品 1,然后产品 1 的文章应该出现。 When I click another product, let's say Product 2 then Product 1's articles should be hidden and Product 2's articles are shown.
当我点击另一个产品时,假设产品 2,那么产品 1 的文章应该隐藏,产品 2 的文章显示。
Like this:像这样:
CASE 1: When Product 1 is clicked案例 1:点击产品 1 时
Product 1产品一
Product 1's articles <--- Show div when page is loaded.产品 1 的文章<--- 在页面加载时显示 div。
Product 2产品二
Product 3产品 3
Product 4产品 4
CASE 2: When Product 2 is clicked案例 2:点击产品 2 时
Product 1 <--- Hide div产品 1 <--- 隐藏 div
Product 2产品二
Product 2's articles <--- Show div产品 2 的文章<--- 显示 div
Product 3产品 3
Product 4产品 4
I have tried using the accordion by Boostrap Vue but this did not suit my needs in other requirements.我曾尝试使用 Boostrap Vue 的手风琴,但这不符合我在其他要求中的需求。 This is what I have so far:
这是我到目前为止:
<div class="container">
<div
v-for="(product, productIndex) in productsandarticles"
:key="productIndex"
>
<div class="articlelistingdesktop">
<div class="productlisting"> <--- Product Names Div
{{ product[0].productNames['en'] }}
</div>
<div class="articlelisting"> <--- Product Articles Div
<h4>{{ product[0].productNames['en'] }} 's articles</h4>
<div
v-for="(productInner, productInnerIndex) in product"
:key="productInnerIndex"
>
<nuxt-link :to="'/' + productInner.title_slug"> . <--- Link to the articles
{{ productInnerIndex + 1 }}. {{ productInner.title['en'] }}
</nuxt-link>
</div>
</div>
</div>
</div>
</div>
All my data is coming from a JSON file and currently, my code shows all product names and their respective articles.我的所有数据都来自一个 JSON 文件,目前,我的代码显示了所有产品名称及其各自的文章。 I am trying to also avoid using jquery for my project.
我也试图避免在我的项目中使用 jquery。 I would appreciate if someone could help.
如果有人可以提供帮助,我将不胜感激。
Thanks!谢谢!
Just use reactive property to track index of "active" product...只需使用反应性属性来跟踪“活动”产品的索引...
In your component:在您的组件中:
data() {
return {
activeProductIndex: null
}
}
<div class="container">
<div
v-for="(product, productIndex) in productsandarticles"
:key="productIndex"
>
<div class="articlelistingdesktop">
<div class="productlisting" @click="activeProductIndex = productIndex">
{{ product[0].productNames['en'] }}
</div>
<div class="articlelisting" v-if="activeProductIndex === productIndex">
<h4>{{ product[0].productNames['en'] }} 's articles</h4>
<div
v-for="(productInner, productInnerIndex) in product"
:key="productInnerIndex"
>
<nuxt-link :to="'/' + productInner.title_slug"> . <--- Link to the articles
{{ productInnerIndex + 1 }}. {{ productInner.title['en'] }}
</nuxt-link>
</div>
</div>
</div>
</div>
</div>
Changes in template explained:模板更改说明:
<div class="productlisting" @click="activeProductIndex = productIndex">
- using click event to assign current product index into activeProductIndex
variable <div class="productlisting" @click="activeProductIndex = productIndex">
- 使用点击事件将当前产品索引分配给activeProductIndex
变量
<div class="articlelisting" v-if="activeProductIndex === productIndex">
- render article listing only if product is "active" <div class="articlelisting" v-if="activeProductIndex === productIndex">
- 仅在产品“活动”时呈现文章列表
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.