CodeNinja
CodeNinja

Reputation: 3278

Changing the size of a jquery UI dialog dynamically

I have a jquery dialog . I am displaying an asp.net gridview within the dialog. I want the size of the dialog to change based on the size of the grid view.

There is a button, which shows the dialog when its clicked.

I want to set the size of the dialog such that the gridview fits in it perfectly.

   I have my javascript code below : 



 $("#ViewModalPopup").dialog({
                height: 800px,
                scrollable: true,
                width: 800,
                modal: true

            });

Here #ViewModalPopup is the div that encloses the modal popup.

I tried implementing the following logic to adjust the height of the dialog based on the size of the div :

var maxHeight = 600;
            var currentHeight = $('#ViewModalPopup').height();

if (currentHeight < maxHeight) {
                var desiredHeight = currentHeight
                }
            else
            {
                var desiredHeight = maxHeight;
                }

  $("#ViewModalPopup").dialog({
                    height: desiredheight,
                    scrollable: true,
                    width: 800,
                    modal: true

                });

But it is not working as

var currentHeight = $('#ViewModalPopup').height();

is coming out to be null from the second button click onwards.

Is there any way I can change the size of the dialog dynamically ?

Upvotes: 7

Views: 27622

Answers (2)

Awadhesh Singh
Awadhesh Singh

Reputation: 59

/* set dynamic height of modal popup and scroll according to window height */
function setModalMaxHeight(element) {
    this.$element = $(element);
    this.$content = this.$element.find('.modal-content');
    var borderWidth = this.$content.outerHeight() - this.$content.innerHeight();
    var dialogMargin = $(window).width() < 768 ? 20 : 60;
    var contentHeight = $(window).height() - (dialogMargin + borderWidth);
    var headerHeight = this.$element.find('.modal-header').outerHeight() || 0;
    var footerHeight = this.$element.find('.modal-footer').outerHeight() || 0;
    var maxHeight = contentHeight - (headerHeight + footerHeight);

    this.$content.css({
        'overflow': 'hidden'
    });

    this.$element.find('.modal-body').css({
        'max-height': maxHeight,
        'overflow-y': 'auto'
    });
}
$('.modal').on('show.bs.modal', function () {
    $(this).show();
    setModalMaxHeight(this);
});
$(window).resize(function () {
    if ($('.modal.in').length != 0) {
        setModalMaxHeight($('.modal.in'));
    }
});

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 122026

Set like

 $("#ViewModalPopupDiv1").dialog("option", "maxHeight", 600);

API

Upvotes: 9

Related Questions