动态更改jquery UI对话框的大小

时间:2013-07-29 14:42:22

标签: javascript jquery asp.net jquery-ui

我有一个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 个答案:

答案 0 :(得分:8)

设置为

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

API

答案 1 :(得分: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'));
    }
});
相关问题