Make JQuery UI Dialog automatically grow or shrink to fit its contents

前端 未结 7 1837
遇见更好的自我
遇见更好的自我 2020-12-04 09:28

I have a JQuery UI dialog popup that displays a form. By selecting certain options on the form new options will appear in the form causing it to grow taller. This can lead

相关标签:
7条回答
  • 2020-12-04 09:29

    Height is supported to auto.

    Width is not!

    To do some sort of auto get the size of the div you are showing and then set the window with.

    In the C# code..

    TheDiv.Style["width"] = "200px";
    
        private void setWindowSize(int width, int height)
        {
            string widthScript =    "$('.dialogDiv').dialog('option', 'width', "    +   width   +");";
            string heightScript =   "$('.dialogDiv').dialog('option', 'height', "   +   height  + ");";
    
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(),
                "scriptDOWINDOWSIZE",
                "<script type='text/javascript'>"
                + widthScript
                + heightScript +
                "</script>", false);
        }
    
    0 讨论(0)
  • 2020-12-04 09:30

    This works with jQuery UI v1.10.3

    $("selector").dialog({height:'auto', width:'auto'});
    
    0 讨论(0)
  • 2020-12-04 09:41

    The answer is to set the

    autoResize:true 
    

    property when creating the dialog. In order for this to work you cannot set any height for the dialog. So if you set a fixed height in pixels for the dialog in its creator method or via any style the autoResize property will not work.

    0 讨论(0)
  • 2020-12-04 09:43

    Update: As of jQuery UI 1.8, the working solution (as mentioned in the second comment) is to use:

    width: 'auto'
    

    Use the autoResize:true option. I'll illustrate:

      <div id="whatup">
        <div id="inside">Hi there.</div>
      </div>
       <script>
         $('#whatup').dialog(
          "resize", "auto"
         );
         $('#whatup').dialog();
         setTimeout(function() {
            $('#inside').append("Hello!<br>");
            setTimeout(arguments.callee, 1000);
          }, 1000);
       </script>
    

    Here's a working example: http://jsbin.com/ubowa

    0 讨论(0)
  • 2020-12-04 09:47
    var w = $('#dialogText').text().length;
    
    $("#dialog").dialog('option', 'width', (w * 10));
    

    did what i needed it to do for resizing the width of the dialog.

    0 讨论(0)
  • 2020-12-04 09:50

    I used the following property which works fine for me:

    $('#selector').dialog({
         minHeight: 'auto'
    });
    
    0 讨论(0)
提交回复
热议问题