Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Dialog auto-resize on dynamic content and maintain center position

I have a jQuery Dialog box and Am loading the content using Ajax.

The Dialog is initially in the center of the page. The problem , here is , Since its a dynamic content, the size of the data is unknown. So, When I get large data, the dialog grows only in the bottom side i.e. The dialog expands in the bottom, and the top is still in the same position.

What I want is

When the data is loaded, the dialog should expand in both the direction (top and bottom ), so that the content is visible without scrolling.

like image 682
madhairsilence Avatar asked Jun 27 '12 05:06

madhairsilence


3 Answers

None of the answers I found on the internet satisfied me while I was looking at the solution. Even this one doesn't. After reading a lot more about the JQuery API documentation, I found something really interesting. As this web page describe, you can bind an event in which it will be executed after the ajax request has done its job. Thing is, it's not simple as this; As I was doing my own tests, using the example provided in the API documentation, I couldn't make it to work. It seems like my JQuery dialog didn't exist in a "future" context manner.

This led me to this page which description was: Attach an event handler for all elements which match the current selector, now and in the future. Finding this leads me to create a function like this one:

$(document).live("ajaxStop", function (e) {
      $(".myDiagDiv").dialog("option", "position", "center");
});

And voila! It works like a charm! After the ajax request is done, the position property is changed and adapted to the content of the div and its dimensions!

Hope it helps people in the future!

EDIT: You might want to use the function ".on()" instead of ".live()". Since the time I wrote my answer, it seems like the function ".live()" as been removed in version 1.9 of jQuery and replaced by the new one. A more appropriate solution for users of jQuery >= 1.9 would be something like this:

$(document).on("ajaxStop", function (e) {
      $(".myDiagDiv").dialog("option", "position", "center");
});
like image 75
David Avatar answered Oct 14 '22 18:10

David


The default dialog is absolutely relative positioned.

The dialog may expand on giving auto height, but when the page is scrolled , the dialog is reposition itself.

The only way to do this is, apply these styles to the dialog

  1. Position the dialog in the Window by

    position : ['center',<distance from top>]

  2. Fix the position by using css style

    .fixed-dialog { position:"fixed" }

  3. Add this class to the dialog

    dialogClass : 'fixed-dialog'

So, the dialog would look like

$('#dialog-div').dialog({
        position : ['center',10],
        dialogClass: "fixed-dialog"
    });
like image 40
madhairsilence Avatar answered Oct 14 '22 16:10

madhairsilence


 Use this
    modal: true,
    width: '80%',
    autoResize:true,
    resizable: true,
    position: {
        my: "center top", 
        at: "center top", 
        of: window
    }

order must be same

like image 30
Mohit Singh Avatar answered Oct 14 '22 18:10

Mohit Singh