bootstrap icon indicating copy to clipboard operation
bootstrap copied to clipboard

Support for loading remote data (like facebox) in modal window

Open ravikanchikare opened this issue 14 years ago • 47 comments

Add support for loading remote data (like facebox) on model window

ravikanchikare avatar Jan 09 '12 05:01 ravikanchikare

Yeah, have been thinking about that. I'll look into it.

fat avatar Jan 09 '12 07:01 fat

that would be super cool.

huzzi avatar Jan 09 '12 14:01 huzzi

+1

t1mmen avatar Jan 20 '12 13:01 t1mmen

+1

WaYdotNET avatar Jan 23 '12 20:01 WaYdotNET

Another +1 here. I asked about this in version 1.x so would be good to get it in.

martinbean avatar Jan 23 '12 21:01 martinbean

Well, since modals can get some. Tabs could use some ajaxifying, too :)

pokonski avatar Jan 23 '12 21:01 pokonski

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>

WaYdotNET avatar Jan 23 '12 21:01 WaYdotNET

thx

Nelrann avatar Jan 30 '12 21:01 Nelrann

+1

quangbahoa avatar Feb 01 '12 16:02 quangbahoa

+1 please.

jefftriplett avatar Feb 10 '12 00:02 jefftriplett

+1

robertoaloi avatar Feb 21 '12 12:02 robertoaloi

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">&times;</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);

toshsan avatar Feb 21 '12 13:02 toshsan

-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.

andriijas avatar Feb 22 '12 14:02 andriijas

@WaYdotNET

Thanks for the code, how would I place the ajax data inside the modal container?

`

`

huzzi avatar Feb 23 '12 16:02 huzzi

jeez man. $('#myModal .modal-body').load(url)

andriijas avatar Feb 23 '12 16:02 andriijas

Hey, is there a quick hack so links inside the modal window open in the modal too ?

rtpm avatar Feb 25 '12 20:02 rtpm

@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 avatar Feb 25 '12 20:02 martinbean

@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

rtpm avatar Feb 26 '12 13:02 rtpm

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); });

andriijas avatar Feb 26 '12 18:02 andriijas

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.

jmathai avatar Mar 17 '12 07:03 jmathai