如何在Vue.js中根据另一个div中的点击来显示和隐藏一个div?

huangapple go评论69阅读模式
英文:

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的文章 <--- 页面加载时显示。

  1. Product 1: Article 1
  2. Product 1: Article 2

Product 2

Product 3

Product 4


案例2:点击Product 2时

Product 1 <--- 隐藏div

Product 2

Product 2的文章 <--- 显示div

  1. Product 2: Article 1
  2. 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">                             &lt;--- 产品名称Div
          {{ product[0].productNames['en'] }}
        </div>
        <div class="articlelisting">                             &lt;--- 产品文章Div
          <h4>{{ product[0].productNames['en'] }} 's articles</h4>
          <div
            v-for="(productInner, productInnerIndex) in product"
            :key="productInnerIndex"
          >
            <nuxt-link :to="'/' + productInner.title_slug"> .    &lt;--- 文章链接
              {{ 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.

  1. Product 1: Article 1
  2. 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

  1. Product 2: Article 1
  2. 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:

&lt;div class=&quot;container&quot;&gt;
    &lt;div
      v-for=&quot;(product, productIndex) in productsandarticles&quot;
      :key=&quot;productIndex&quot;
    &gt;
      &lt;div class=&quot;articlelistingdesktop&quot;&gt;
        &lt;div class=&quot;productlisting&quot;&gt;                             &lt;--- Product Names Div
          {{ product[0].productNames[&#39;en&#39;] }}
        &lt;/div&gt;
        &lt;div class=&quot;articlelisting&quot;&gt;                             &lt;--- Product Articles Div
          &lt;h4&gt;{{ product[0].productNames[&#39;en&#39;] }} &#39;s articles&lt;/h4&gt;
          &lt;div
            v-for=&quot;(productInner, productInnerIndex) in product&quot;
            :key=&quot;productInnerIndex&quot;
          &gt;
            &lt;nuxt-link :to=&quot;&#39;/&#39; + productInner.title_slug&quot;&gt; .    &lt;--- Link to the articles
              {{ productInnerIndex + 1 }}. {{ productInner.title[&#39;en&#39;] }}
            &lt;/nuxt-link&gt;
          &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

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"> .    &lt;--- 链接到文章
              {{ 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
  }
}
&lt;div class=&quot;container&quot;&gt;
    &lt;div
      v-for=&quot;(product, productIndex) in productsandarticles&quot;
      :key=&quot;productIndex&quot;
    &gt;
      &lt;div class=&quot;articlelistingdesktop&quot;&gt;
        &lt;div class=&quot;productlisting&quot; @click=&quot;activeProductIndex = productIndex&quot;&gt;                
          {{ product[0].productNames[&#39;en&#39;] }}
        &lt;/div&gt;
        &lt;div class=&quot;articlelisting&quot; v-if=&quot;activeProductIndex === productIndex&quot;&gt;
          &lt;h4&gt;{{ product[0].productNames[&#39;en&#39;] }} &#39;s articles&lt;/h4&gt;
          &lt;div
            v-for=&quot;(productInner, productInnerIndex) in product&quot;
            :key=&quot;productInnerIndex&quot;
          &gt;
            &lt;nuxt-link :to=&quot;&#39;/&#39; + productInner.title_slug&quot;&gt; .    &lt;--- Link to the articles
              {{ productInnerIndex + 1 }}. {{ productInner.title[&#39;en&#39;] }}
            &lt;/nuxt-link&gt;
          &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

Changes in template explained:

&lt;div class=&quot;productlisting&quot; @click=&quot;activeProductIndex = productIndex&quot;&gt; - using click event to assign current product index into activeProductIndex variable

&lt;div class=&quot;articlelisting&quot; v-if=&quot;activeProductIndex === productIndex&quot;&gt; - render article listing only if product is "active"

huangapple
  • 本文由 发表于 2020年1月3日 17:07:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定