Support for loading remote data (like facebox) in modal window
Add support for loading remote data (like facebox) on model window
Yeah, have been thinking about that. I'll look into it.
that would be super cool.
+1
+1
Another +1 here. I asked about this in version 1.x so would be good to get it in.
Well, since modals can get some. Tabs could use some ajaxifying, too :)
Actaully i'm use this code to load remote data:
$('.to_modal').click(function(e) {
e.preventDefault();
var href = $(e.target).attr('href');
if (href.indexOf('#') == 0) {
$(href).modal('open');
} else {
$.get(href, function(data) {
$('<div class="modal fade" >' + data + '</div>').modal();
});
}
});
and
<a herf="/url/to/load" class="to_modal">link</a>
thx
+1
+1 please.
+1
I have written this little plugin for the same. this might work for some usecases.
// plug-ins
(function($){
$.fn.popup = function(title, url, callback, func){
this.click(function(evt){
evt.preventDefault();
var id = $(this).attr("id") + "-modal";
var modal = $("#" + id);
if(modal.length<1){
modal = $('<div class="modal hide"><div class="modal-header"><a class="close" data-dismiss="modal">×</a><h3 class="modal-title"></h3></div><div class="modal-body"></div><div class="modal-footer"><a href="#" class="btn btn-primary">Add</a><a href="#" class="btn dlClose">Close</a></div></div>')
.attr("id", id);
$(".modal-title", modal).text(title);
var args = (func !=undefined)? func():{};
$.get(url, args, function(form){
$(".modal-body", modal).html(form);
$(".dlClose", modal).click(function(){ modal.modal('hide');});
var frm = $("form", modal);
$(".btn-primary", modal).click(function(){ frm.submit(); });
frm.validate({submitHandler: function() {
var data = frm.serialize();
$.post(frm.attr("action"), data, function(data){
callback(data);
modal.modal('hide');
modal.remove();
});
}});
modal.modal();
});
}
else{
modal.modal();
}
});
};
})(jQuery);
-10
In my opinion the coolness of the bootstrap plugins is because of its simplicity. Tabs is just tabbable tabs, modal is just modal.
I think things like what content goes in to the modal or tabs doesn't belong in the plugin. Just look at how bloated the jquery ui code has become.
People tend to forget how easy it is to write wrappers and plugins on their own of such basic things like loading remote stuff.
@WaYdotNET
Thanks for the code, how would I place the ajax data inside the modal container?
`
Modal header
{DATA HERE}
jeez man. $('#myModal .modal-body').load(url)
Hey, is there a quick hack so links inside the modal window open in the modal too ?
@rtpm I'm not sure off the top of my head, but you should be able to do in jQuery if you add something similar to the following after your modal declarations in your JavaScript:
$('.modal.body').each(function() {
var modalBody = this;
$(modalBody).find('a').click(function(e) {
e.preventDefault();
$(modalBody).load(this.href);
});
});
Like I say, untested but basically you want to put an event listener on all <a> elements in your modal body, and then replace the content of the modal body with the target URL of your <a> tag.
@martinbean Thanks. I've tried with a code above from @WaYdotNET but it doesn't seem to work. I've changed the selector, but I think there needs to be a live click listener set up for click. No matter where I put the code it doesn't seem to work. No click event is attached to the links inside the modal. Let me also mention that I'm a javascript (and so jQuery) noob. Thanks
I would do this to cover modals injected with ajax $(document).on('click', '.modal-body a', function(e) { e.preventDefault(); $(this).parents('.modal-body').load(this.href); });
or this if you dont need to support ajax injected modals
$('.modal-body').on('click', 'a', function(e) { e.preventDefault(); $(this).parents('.modal-body').load(this.href); });
I originally came looking for how to do this but after reading the comments I believe the pros of keeping bootstrap simple outweigh the cons of adding complexity. It's fairly trivial to add your own wrapper around the modal to populate a div with remote content. There's like 5 ways to do it with jQuery.