首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何根据v-for值在我的模式窗口(单击按钮时)上获取所选数据?

如何根据v-for值在我的模式窗口(单击按钮时)上获取所选数据?
EN

Stack Overflow用户
提问于 2019-09-18 20:29:20
回答 3查看 78关注 0票数 0

我是Vue的新手,正在使用Bootstrap modals来显示产品信息。我有一个网格容器,每个容器都有一个产品图片、描述和两个按钮。其中一个按钮(More details >>),当被点击时,会弹出一个模式窗口,其中应该显示与它所在的网格完全相同的产品描述和图片。

代码语言:javascript
复制
<div id="myapp">
  <h1> {{ allRecords() }} </h1>
  <div class="wrapper" >
    <div class="grid-container" v-for="product in products" v-bind:key="product.ID">
      <div class="Area-1">
        <img class="product_image" src="https:....single_product.jpg"> 
      </div>
      <div class="Area-2">
        <div class = "amount">
          {{ product.amount }}
        </div>
        {{ product.Description }}
      </div>
      <div class="Area-3">
        <b-button size="sm" v-b-modal="'myModal'" product_item = "'product'">
          More Details >>
        </b-button>
        <b-modal id="myModal" >
          <h1> {{ product.Name }} </h1>
          <h3> {{ product.Description }} </h3>
        </b-modal>
      </div>
      <div class="Area-4">
        <br><button>Buy</button>
      </div>
    </div>
  </div>
</div>
代码语言:javascript
复制
var app = new Vue({
  'el': '#myapp',
  data: {
    products: "",
    productID: 0
  },
  methods: {
    allRecords: function(){
      axios.get('ajaxfile.php')
        .then(function (response) {
          app.products = response.data;
        })
        .catch(function (error) {
          console.log(error);
        });
    },
  }
})

区域1、区域2和区域4工作得很好,它们分别根据每个网格容器的v-for值和预期显示产品数据。区域3是一个问题,当我点击More details >>按钮时,我只看到一个褪色的黑屏。我不确定我在这里做错了什么,真的很感谢你的帮助。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-09-18 20:40:32

添加属性selectedProduct,然后在More Details按钮上单击event,将当前产品分配给selectedProduct成员,如下所示:

HTML

代码语言:javascript
复制
  <div class="Area-3">
    <b-button size="sm" v-b-modal="'myModal'" 
             @click="selectProduct(product)">More Details >> </b-button>    
    <b-modal id="myModal">
             <h1> {{ this.selectedProduct.Name }} </h1>
             <h3> {{ this.selectedProduct.Description }} </h3>
    </b-modal>
  </div>

Javascript:

代码语言:javascript
复制
var app = new Vue({
 'el': '#myapp',
 data: {
    products: "",
    productID: 0,
    selectedProduct: {Name: '', Description: '', Amount:0}
 },
 methods: {
   allRecords: function(){
   ...
   },
   selectProduct: function(product)
   {
       this.selectedProduct = product;
   }
   ...
 }
票数 1
EN

Stack Overflow用户

发布于 2019-09-18 20:56:39

你看到黑屏的原因是因为你没有给v-for中的b-modal一个唯一的ID。所以当你点击按钮时,它实际上是同时打开所有的模态,并堆叠背景使其看起来非常暗。

相反,您可以在您的模式ID中使用您的产品ID (我猜它是唯一的)来使其唯一

代码语言:javascript
复制
<div id="myapp">
  <h1> {{ allRecords() }} </h1>
  <div class="wrapper" >
    <div class="grid-container" v-for="product in products" v-bind:key="product.ID">
      <div class="Area-1">
        <img class="product_image" src="https:....single_product.jpg"> 
      </div>
      <div class="Area-2"><div class = "amount">{{ product.amount }} </div>
        {{ product.Description }}
      </div>
      <div class="Area-3">
        <b-button size="sm" v-b-modal="`myModal-${product.ID}`" product_item = "'product'">
          More Details >> 
        </b-button>
        <b-modal :id="`myModal-${product.ID}`" >
          <h1> {{ product.Name }} </h1>
          <h3> {{ product.Description }} </h3>
        </b-modal>
      </div>
      <div class="Area-4">
        <br><button>Buy</button>
      </div>
    </div>
  </div>
</div>

示例钢笔:https://codepen.io/Hiws/pen/qBWJjOZ?editors=1010

票数 1
EN

Stack Overflow用户

发布于 2019-09-18 21:17:44

我不能重复这个问题。我创建了JSFiddle来测试:

https://jsfiddle.net/4289wh0e/1/

然而,我意识到当我单击"More Details“按钮时,会显示多个模态元素。

我建议您只在包装器中添加一个模型,并将选择的产品存储在数据变量中。

https://jsfiddle.net/4289wh0e/2/

代码语言:javascript
复制
<div id="myapp">
    <h1> {{ allRecords() }} </h1>

    <div class="wrapper">

        <div class="grid-container" v-for="product in products" v-bind:key="product.ID">

            <div class="Area-1"><img class="product_image" src="https:....single_product.jpg"> </div>

            <div class="Area-2">
                <div class="amount">{{ product.amount }} </div>
                {{ product.Description }}</div>

            <div class="Area-3">
                <b-button size="sm" v-b-modal="'productModal'" @click="chooseProduct(product)" product_item="'product'">More Details >> </b-button>

            </div>

            <div class="Area-4">
                <br>
                <button>Buy</button>
            </div>
        </div>

        <b-modal id="productModal" v-if="chosenProduct">
            <h1> {{ chosenProduct.Name }} </h1>
            <h3> {{ chosenProduct.Description }} </h3>
        </b-modal>
    </div>
</div>
代码语言:javascript
复制
Vue.use(BootstrapVue)

var app = new Vue({
    'el': '#myapp',
    data: {
        products: [],
        chosenProduct: null
    },
    methods: {
            chooseProduct: function (product) {
            this.chosenProduct = product
        },
        allRecords: function(){
                    this.products = [
            {
                ID: 1,
                Description: 'dek',
              Name: 'Name',
              amount: 100
            },
            {
                ID: 2,
                Description: 'dek 2',
              Name: 'Name 2',
              amount: 300
            }
          ]
        },
    }
})
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57992861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档