0

I am trying to render a modal with a simple change in my data using a vue @click on an element but I'm not able to get the modal to popup. I am hoping someone can point me in the right direction here.

I'm not using vue-bootstrap in this project just bootstrap 4 for reference here.

my click


  <div class="d-flex justify-content-between">
       <span class="d-block">These students cannot be in the same group:</span>
       <button @click="showModal = true" class="btn btn-outline-primary btn-sm">Add constraint group</button>
  </div>

my Modal


<!--MODAL SECTION-->
        <div v-if="showModal" class="modal" id="myModal" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Modal title</h5>
                             <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                    <div class="modal-body">
                         <p>Modal body text goes here.</p>
                    </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-primary">Save changes</button>
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        </div>
                    </div>
                </div>
            </div>
<!--MODAL SECTION-->

my data

data:{
otherData: [],
showModal: false
}
4
  • Can you please specify how it is showing now? Commented Apr 9, 2020 at 4:11
  • currently it isn't showing at all. I am trying to use an @click to change the showModal prop in data to 'true' but that doesn't seem to be the right way of doing it. Commented Apr 9, 2020 at 4:51
  • 1
    Have you chceck $('#myModal').modal('show') is working? Commented Apr 9, 2020 at 4:56
  • @Rjosh yes I was able to get it to work eventually by using this after making sure js popper was included properly Commented Apr 9, 2020 at 5:59

1 Answer 1

2

You need data-toggle and data-target on the button:

<div class="d-flex justify-content-between">
    <span class="d-block">These students cannot be in the same group:</span>
    <button data-target="#myModal" data-toggle="modal" @click="showModal = true" class="btn btn-outline-primary btn-sm">Add constraint group</button>
</div>

See: https://getbootstrap.com/docs/4.0/components/modal/#via-data-attributes

Since the modal is controlled via some bootstrap internals, I do not think you need the v-if and @click handler.

Sign up to request clarification or add additional context in comments.

4 Comments

I haven't been able to get it to work using either the v-if or the data-target method unfortunately. I have also tried using v-model to no effect. I know bootstrap is included. Does js popper need to be included to use bootstrap modals?
after including js popper the modal now works for some reason. Thanks for your help!
@monsterpiece awesome! What version of bootstrap 4 are you using? Popper was required for modals in the beta versions, but I do not think it is required in the current version (4.4.1). Some CDNs serve older versions.
I was using 4.4.1 I might be mistaken and I made a fluke change by accident but that seemed to be the fix for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.