7

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 ?

3
  • What happens when you don't set a height at all? Commented Jul 29, 2013 at 14:44
  • Hi Jason P ! Sometimes the gridview has so many rows that it stretches from the top to the bottom of the screen. Setting a height and putting a scrollbar helps in limiting the height of the dialog. Commented Jul 29, 2013 at 15:19
  • So you basically want a max height. Have you tried setting the max-height on your dialog div using css? Commented Jul 29, 2013 at 15:39

2 Answers 2

9

Set like

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

API

Sign up to request clarification or add additional context in comments.

2 Comments

Hey Thanks ! $("#ViewModalPopupDiv1").dialog("option", "maxHeight", 600); was actually what I was looking for . Thanks for pointing me in the right direction.
maxheight and not height ?
0
/* 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'));
    }
});

1 Comment

Add some description

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.