Search code examples
jqueryjquery-ui-dialog

Auto height on UI Dialog, if height changes dialog is no longer vertically centred


I have the below function which loads a URL into my dialog, when that URL is opened, a function is run on document ready, that function shows or hides divs depending on a value. that in turn expands the dialog, the auto height works, but the dialog is expanding downward and is no longer centered.

so Dialog Open > URL open in dialog > show panel run > dialog expands height > dialog is no longer centered.

CODE TRIED TO AUTO RE CENTER

$.ui.dialog.prototype.options.autoReposition = true;
$(window).resize(function () {
    $(".editDialog").dialog("option", "position", "center");
    $(".ui-dialog-content:visible").each(function () {
        $(this).dialog("option", "position", $(this).dialog("option", "position"));
    });
});
$(".ui-dialog-content").resize(function () {
    $(".ui-dialog-content:visible").each(function () {
        $(this).dialog("option", "position", $(this).dialog("option", "position"));
    });
});

DIALOG CODE

$(document).ready(function () {
    var width = $(window).width() / 2;
    $.ajaxSetup({ cache: false });

$(".editDialog").live("click", function (e) {
        var url = $(this).attr('href');
        $("#dialog-edit").dialog({
            title: 'Edit',
            autoOpen: false,
            resizable: true,
            autoResize:true,
            minHeight: 'auto',
            width: width,
            modal: true,
            draggable: true,
            open: function (event, ui) {
                //Show the loading div on open.
                $("#dvLoading").show();
                //adding a callback function wich will be launched after the loading
                $(this).load(url,function(response, status, xhr) {
                    if (status == "error") {
                        var msg = "Sorry but there was an error: ";
                        $(this).html(msg + xhr.status + " " + xhr.statusText);
                    } else $("#dvLoading").hide();
                });
            },
            close: function (event, ui) {
                $(this).dialog('close');
            }
        });

        $("#dialog-edit").dialog('open');
        return false;
    });

URL DOCUMENT READY SHOW/HIDE DIVS

$(document).ready(function () {
    showPanel()
});
function showPanel()
{
    var panel = $("#lstAssetTypes").val();
    panel = panel.substr(panel.indexOf(",") + 1)

    switch (panel)
    {
        case "User":
            $("#dvWKS").show();
            $("#dvNTW").hide();

            break;
        case "Network":
            $("#dvWKS").hide();
            $("#dvNTW").show();
            break;
        default:
            break;
    }
}

HTML for Divs called

 <div id="dialog-confirm" style="display: none">
    <p>
        <span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>
        Are you sure to delete?
    </p>
</div>
<div id="dialog-edit" style="display: none">
    <div id="dvLoading" class="loading"><img src="~/Images/loading.gif"><p>Loading please wait...</p></div>
</div>
<div id="dialog-view" style="display: none">
</div>

Solution

  • It seems that automatically detecting height changes is not possible, there is no event for that. Instead, you need to call the reposition code manually. Use a function like this:

    var reposition_dialog = function() {
        $("#dialog-edit").dialog("option", "position", "center");
            $(".ui-dialog-content:visible").each(function () {
            $(this).dialog("option", "position", $(this).dialog("option", "position"));
        });
    }
    

    (I changed .editDialog from your code to #dialog-edit as it seemed like an error)

    Just call the function when you are done changing the contents:

    $(document).ready(function () {
        showPanel()
    });
    function showPanel()
    {
        var panel = $("#lstAssetTypes").val();
        panel = panel.substr(panel.indexOf(",") + 1)
    
        switch (panel)
        {
            case "User":
                $("#dvWKS").show();
                $("#dvNTW").hide();
    
                break;
            case "Network":
                $("#dvWKS").hide();
                $("#dvNTW").show();
                break;
            default:
                break;
        }
        reposition_dialog();
    }
    

    If you really want to detect it automatically, which i would only do if for some reason you can't predict when content is going to change, take a look at this thread: Detect element content changes with jQuery