Изменение размера диалогового окна jQuery интерфейса динамически

у меня есть диалог jquery . Я показываю asp.net gridview в диалоговом окне. Я хочу, чтобы размер окна будет меняться в зависимости от размера сетки.

есть кнопка, которая показывает диалоговое окно при его нажатии.

Я хочу установить размер диалога таким образом, чтобы gridview идеально вписывался в него.

   I have my javascript code below : 



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

            });

здесь #ViewModalPopup-это div, который заключает модальное всплывающее окно.

Я попытался реализовать следующую логику для отрегулируйте высоту диалогового окна в зависимости от размера 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

                });

но он не работает как

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

выходит, чтобы быть нулевым со второй кнопки нажмите и далее.

есть ли способ динамически изменить размер диалога ?

2 ответов


установить как

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

API


/* 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'));
    }
});