0

I'd like to show a modal-component after an "on click"-Event is triggered. Is it possible to show a component using a method call? Or what is the best practice in this case?

My use case is as follows:

I have multiple cards, which contain several information about something. When the user clicks on one card a modal-component should pop up and show more details about this card.

<div class="card" @click="showDetails()">
<h3 class="header">{{ Name }} {{ Type }}</h3>
<div class="container">
  some Information<br>
  more Information<br>
</div>



export default {
  props: {
    job: Object
  },
  components: {
  },
  methods: {
    showDetails() {

    }
  },
  name: "card"
};
</script>
4
  • 2
    please share the relevant parts of your code Commented Aug 19, 2020 at 15:04
  • after @click a method call is required. I don't know how to open a component from a method call. Do you know? Commented Aug 19, 2020 at 15:09
  • 1
    it will be easier to help you if you can share your code or a fiddle or sandbox. Commented Aug 19, 2020 at 15:13
  • @DisDes, Are you willing to use vue bootstrap? Commented Aug 19, 2020 at 17:40

4 Answers 4

3

For example. Create modal with your component inside (i use bootstrap-vue)

 <b-modal ref="my-component" >
     <your-component></your-component>
 </b-modal>

And add event to @click method

this.$refs.my-component.show();
Sign up to request clarification or add additional context in comments.

2 Comments

I only want to show the modal if the user clicks on the card
Hmm, ok. You have some modal and method showDetails() that show your modal. Lets add "ref" to your modal: <modal ref="modal"></modal, then add this code: this.$refs.modal.show() to your method showDetails().
2

Usually you want to use a Bool to control whether a modal is visible or not. Simply use @click (v-on:click) to toggle Bool.

In Data:

modal: false

Then on a button:

<button @click="modal = !modal">Click me to toggle modal</button>

Edit: Forgot to add logic on modal:

<modal v-model="modal"></modal>

The v-model simply means that it doesn't show if it's false and it does if it's true.

More info: https://v2.vuejs.org/v2/api/#v-model

I hope this is sufficient.

Piece of advice: Next time give a better explanation with more code. Otherwise it will become guesswork for everyone who wants to answer.

Comments

1

The question seem to have an extra logic, since you have multiple cards with different items, you want to open a modal with a single item info each time, and probably you don't want to create N modals for all the records displayer in cards.

As the previous answers state, you CAN open a modal by calling a method, and also you can open a modal replacing a variable value that allows it, but also you need to close the modal and that is an extra logic you must have in mind.

you can have an event directive as you have here, and also your modal code (component most of the times):

<div class="card" @click="showDetails()">

<my-modal-component v-show="openModal" >
   <my-card-information-component />
</my-modal-component>

on your script you must declare the property that will trigger the moday display and the method to mutate the property

export default {
    data(){
        return {
           showModal:false
        }
    },
    methods:{
       showDetails(){
           this.showModal = true;
       }
    }
}

to close the modal you have multiple options, you can add a button to the component, click the modal backdrop, etc. But to achieve this you need to emit an event from the child component to the parent component that will update the showModal property back to false and will close the modal.

Comments

1

Here is what worked for me.

Please note, my answer is in Vue3 as of Feb 17, 2022

<script setup>
  // set a boolean ref value as false. we will be using the openModal function to trigger its change
  let showModal = ref(false);

  // create the openModal function and set showModal to true
  const openModal = () => {
    showModal.value = true;
    // console.log(showModal);
  };
</script>  

<template>
  // Here is our modal. It will only display on the page once we click the button that triggers the openModal function
    <Modal v-if="showModal" />
  
  // Here is the button that triggers the openModal function, changing the showModal boolean value to true
    <button v-on:click="reply" type="button">Reply</button>
</template>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.