英文:
How can I show and hide a div based on what's clicked in another div in Vue js?
问题
我有2个div,一个显示产品的名称(Product 1,Product 2...等),另一个显示产品的文章(Product 1: Article 1,Product 2: Article 1,Product 2: Article 2...)。我希望能够实现这样的功能:当我点击Product 1时,Product 1的文章应该显示出来。当我点击另一个产品,比如Product 2,Product 1的文章应该隐藏,Product 2的文章应该显示出来。
像这样:
案例1:点击Product 1时
Product 1
Product 1的文章 <--- 页面加载时显示。
- Product 1: Article 1
- Product 1: Article 2
Product 2
Product 3
Product 4
案例2:点击Product 2时
Product 1 <--- 隐藏div
Product 2
Product 2的文章 <--- 显示div
- Product 2: Article 1
- Product 2: Article 2
Product 3
Product 4
我尝试使用Bootstrap Vue的手风琴组件,但这不符合我的其他需求。以下是我目前的代码:
<div class="container">
<div
v-for="(product, productIndex) in productsandarticles"
:key="productIndex"
>
<div class="articlelistingdesktop">
<div class="productlisting"> <--- 产品名称Div
{{ product[0].productNames['en'] }}
</div>
<div class="articlelisting"> <--- 产品文章Div
<h4>{{ product[0].productNames['en'] }} 's articles</h4>
<div
v-for="(productInner, productInnerIndex) in product"
:key="productInnerIndex"
>
<nuxt-link :to="'/' + productInner.title_slug"> . <--- 文章链接
{{ productInnerIndex + 1 }}. {{ productInner.title['en'] }}
</nuxt-link>
</div>
</div>
</div>
</div>
</div>
我的所有数据都来自一个JSON文件,目前,我的代码显示所有产品名称及其相应的文章。我也在尝试避免在项目中使用jQuery。如果有人能帮助我,我将不胜感激。
谢谢!
英文:
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... ). I would like to have a functionality where I click Product 1 and then Product 1's articles should appear. 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.
Like this:
CASE 1: When Product 1 is clicked
Product 1
Product 1's articles <--- Show div when page is loaded.
- Product 1: Article 1
- Product 1: Article 2
Product 2
Product 3
Product 4
CASE 2: When Product 2 is clicked
Product 1 <--- Hide div
Product 2
Product 2's articles <--- Show div
- Product 2: Article 1
- Product 2: Article 2
Product 3
Product 4
I have tried using the accordion by Boostrap Vue but this did not suit my needs in other requirements. 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. I am trying to also avoid using jquery for my project. I would appreciate if someone could help.
Thanks!
答案1
得分: 1
只需使用响应式属性来跟踪“活动”产品的索引...
在您的组件中:
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"> . <--- 链接到文章
{{ productInnerIndex + 1 }}. {{ productInner.title['en'] }}
</nuxt-link>
</div>
</div>
</div>
</div>
</div>
模板中的更改说明:
<div class="productlisting" @click="activeProductIndex = productIndex">
- 使用点击事件将当前产品索引分配给 activeProductIndex
变量
<div class="articlelisting" v-if="activeProductIndex === productIndex">
- 仅在产品处于“活动”状态时呈现文章列表
英文:
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="articlelisting" v-if="activeProductIndex === productIndex">
- render article listing only if product is "active"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论