简体   繁体   English

如何基于v-for值在模态窗口(单击按钮)上获取所选数据?

[英]How can I get the selected data on my modal window(on button click) based on the v-for value?

I am new to Vue and am using the Bootstrap modals to display product information. 我是Vue的新手,正在使用Bootstrap模式来显示产品信息。 I have grid containers that each have a product picture, description, and two buttons. 我有网格容器,每个容器都有一个产品图片,说明和两个按钮。 One of the buttons( More details >> ), when clicked, would shoot a modal window that should show the very same product description and picture of the grid it was contained in. 单击其中一个按钮( More details >> ),将弹出一个模态窗口,该窗口应显示出与其中所包含的网格完全相同的产品描述和图片。

<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>
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);
        });
    },
  }
})

Area 1, 2 and 4 work perfectly fine and they display the product data according to the v-for value and as expected respectively for each grid container. 区域1、2和4可以很好地工作,它们分别根据v-for值和每个网格容器的预期显示产品数据。 Area 3 is a problem here when I click the More details >> button, I just see a faded black screen. 单击“ More details >>按钮时,这里的区域3是一个问题,我只看到褪色的黑屏。 I am not sure what I am doing wrong here, would really appreciate some help. 我不确定我在这里做错了什么,真的会感谢您的帮助。

Add a property selectedProduct, then on More Details button click event, assign the current product to the selectedProduct member as below : 添加一个属性selectedProduct,然后在More Details按钮上单击事件,将当前产品分配给selectedProduct成员,如下所示:

HTML HTML

  <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;
   }
   ...
 }

The reason you're just seeing a black screen is because you're not giving the b-modal in your v-for a unique ID. 之所以只看到黑屏,是因为您没有在v-for唯一的ID赋予b-modal So when you click the button it's actually opening all the modals at the same time, and stacking the backdrop making it look very dark. 因此,当您单击该按钮时,实际上是同时打开所有模态,并堆叠背景使它看起来很暗。

Instead you could use your product ID (I'm guessing it's unique) in your modal ID to make it unique 相反,您可以在模式ID中使用您的产品ID (我猜它是唯一的)

<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>

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

I can't replicate the issue. 我无法复制该问题。 I created JSFiddle to test: 我创建了JSFiddle进行测试:

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

However, I realized multiple modal elements are displayed when I click on the "More Details" button. 但是,我意识到单击“更多详细信息”按钮时会显示多个模态元素。

I suggest you add only one modal in the wrapper and store the chosen product in a data variable. 我建议您在包装器中仅添加一个模式,并将所选产品存储在数据变量中。

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

<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>
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
            }
          ]
        },
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM