Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery to open Bootstrap v3 modal of remote url

I have a page of links to internal pages I need to open up in a Bootstrap modal DIV. The problem is that it seems that using the latest version of Bootstrap v3 in conjunction with jQuery v2.1.4 just doesn't work when it comes to loading content this way. There's plenty of tutorials I've read about creating modals with Bootstrap and how remote content is being phased out. But there's got to be away to make this work with jQuery, or maybe not.

The theory is that when you click

<a class="" href="/log/viewslim?id=72" title="View" data-target="#myModal" data-toggle="modal">View 72</a> 

the content of data-load-remote is supposed to be read and injected into the div with class modal-body.

<div id="myModal" class="modal fade"> <div class="modal-dialog">     <div class="modal-content">             <div class="modal-header">                 <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>                 <h4 class="modal-title">Event</h4>             </div>             <div class="modal-body">                 <p>Loading...</p>             </div>             <div class="modal-footer">                 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>                 <button type="submit" class="btn btn-primary">Save changes</button>             </div>     </div> </div> 

However, when I try this example with jQuery v2.1.4 and BS v3.3+, what it does is open up a modal window with grey background but all the styling of the modal window is gone. Meaning it seems to only display the modal-body div, but the modal header, pretty modal frame and bottom buttons in modal-footer div are not displayed at all. The only way to close the box is to click outside the modal box.partial modal window

I've found examples all around about how to open up remote urls this way, but they all use outdated version of bootstrap, not the version I'm working with. Can anyone shed some lights on this please?

like image 607
vanarie Avatar asked Dec 29 '15 02:12

vanarie


2 Answers

So basically, in jquery what we can do is to load href attribute using the load function. This way we can use the url in <a> tag and load that in modal-body.

<a  href='/site/login' class='ls-modal'>Login</a>  //JS script $('.ls-modal').on('click', function(e){   e.preventDefault();   $('#myModal').modal('show').find('.modal-body').load($(this).attr('href')); }); 
like image 159
Kijan Maharjan Avatar answered Sep 16 '22 18:09

Kijan Maharjan


From Bootstrap's docs about the remote option;

This option is deprecated since v3.3.0 and has been removed in v4. We recommend instead using client-side templating or a data binding framework, or calling jQuery.load yourself.

If a remote URL is provided, content will be loaded one time via jQuery's load method and injected into the .modal-content div. If you're using the data-api, you may alternatively use the href attribute to specify the remote source. An example of this is shown below:

<a data-toggle="modal" href="remote.html" data-target="#modal">Click me</a> 

That's the .modal-content div, not .modal-body. If you want to put content inside .modal-body then you need to do that with custom javascript.

So I would call jQuery.load programmatically, meaning you can keep the functionality of the dismiss and/or other buttons as required.

To do this you could use a data tag with the URL from the button that opens the modal, and use the show.bs.modal event to load content into the .modal-body div.

HTML Link/Button

<a href="#" data-toggle="modal" data-load-url="remote.html" data-target="#myModal">Click me</a> 

jQuery

$('#myModal').on('show.bs.modal', function (e) {     var loadurl = $(e.relatedTarget).data('load-url');     $(this).find('.modal-body').load(loadurl); }); 
like image 28
worldofjr Avatar answered Sep 17 '22 18:09

worldofjr