0

I'm developing a small online store (Vue.js) I have several products with different names and prices. Each product has a "Details" button.

I want a modal window with the names and prices of this product to appear when I click on the "Details" button. At the moment, I always only show the data from the first product. I do not know how to display the data of the product that I clicked on. I roughly understand that you need to use "this", but so far no solution comes. I use vue-js <slot></slot> in Modal.

In methods:

showModal() {
  let myModal = new bootstrap.Modal(
    document.getElementById('exampleModal'),
    {}
  );
  myModal.show();
},

My button:

<button @click="showModal">Details</button>
Adlet
  • 5
  • 1
  • 5
  • Where and how do you store the items' details? Are you fetching them dynamically or are they static? – Laerte Jun 10 '21 at 18:28
  • @Laerte I use json-server and db.json file where I store all my products in Array. I use axios to get products.data. – Adlet Jun 11 '21 at 07:02

1 Answers1

0

Given the syntax, I’m assuming you’re using Bootstrap 5.

You’d be better off creating a component Vue component for the product details modal that you can pass a product to as a prop, and then it’ll change its content based on the product.

If you have a list of products that you’re iterating over, then you can do something like this:

<!-- ProductList.vue -->
<template>
    <ul>
        <li v-for="product in products" v-bind:key="product.id">
            <span>{{ product.name }}</span>
            <button v-on:click="showDetails(product)">Details</button>
        </li>
    </ul>
    <portal to="modals" v-if="showModal">
        <product-details-modal
            v-bind:product="product"
            v-bind:show="showModal"
            v-on:hide="showModal = false"
        />
    </portal>
</template>

<script>
import ProductDetailsModal from './ProductDetailsModal.vue';

export default {
    components: {
        ProductDetailsModal,
    },
    data() {
        return {
            product: null,
            products: [],
            showModal: false,
        };
    },
    methods: {
        showDetails(product) {
            this.product = product;
            this.showModal = true;
        },
    },
    mounted() {
        // Load products from API or something
        // Save array of product results to this.products
    },
};
</script>

Now when you click the details button, it’ll set the selected product as a data item, as well as showModal true. The product is then passed to the modal component, which can show the details based on that prop:

<!-- ProductDetailsModal.vue -->
<template>
    <div class="modal fade" id="product-details-modal" ref="modalElement">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="product-details-modal-title">Product Details</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <p>Product name: {{ product.name }}</p>
                    <p>Product price: {{ product.price }}</p>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { Modal } from 'bootstrap';
 
export default {
    data() {
        return {
            modalElement: null,
        };
    },
    mounted() {
        this.modalElement = new Modal(this.$refs.modal);

        this.modalElement.addEventListener('hide.bs.modal', this.$emit('hide'));

        if (this.show) {
            this.modalElement.show();
        }
    },
    props: {
        product: {
            required: true,
            type: Object,
        },
        show: {
            default: false,
            required: false,
            type: Boolean,
        },
    },
    watch: {
        show(show) {
            if (this.modalElement) {
                show ? this.modalElement.show() : this.modalElement.hide();
            }
        },
    },
};
</script>
Martin Bean
  • 38,379
  • 25
  • 128
  • 201
  • 1
    Martin, thank you very much for the detailed answer. I can already see the solution :) – Adlet Jun 11 '21 at 07:01
  • Martin, I added the code and changed it a little. Now I have some errors like [Vue warn]: Error in mounted hook: "TypeError: Cannot read property 'classList' of undefined". This is my project: https://github.com/adlettaigulov/online-store – Adlet Jun 11 '21 at 08:50
  • @Adlet Did this help, then…? – Martin Bean Sep 18 '21 at 00:47
  • Hi, @Martin Bean, yes, thank you very much :) – Adlet Sep 19 '21 at 10:07