English 中文(简体)
JqueryUI - Dialog
  • 时间:2024-11-05

JqueryUI - Dialog


Previous Page Next Page  

Dialog boxes are one of the nice ways of presenting information on an HTML page. A dialog box is a floating window with a title and content area. This window can be moved, resized, and of course, closed using "X" icon by default.

jQueryUI provides dialog() method that transforms the HTML code written on the page into HTML code to display a dialog box.

Syntax

The dialog() method can be used in two forms −

$ (selector, context).dialog (options) Method

The dialog (options) method declares that an HTML element can be administered in the form of a dialog box. The options parameter is an object that specifies the appearance and behavior of that window.

Syntax

$(selector, context).dialog(options);

You can provide one or more options at a time using Javascript object. If there are more than one options to be provided then you will separate them using a comma as follows −

$(selector, context).dialog({option1: value1, option2: value2..... });

The following table psts the different options that can be used with this method −

Sr.No. Option & Description
1

If this option is set to false, it will prevent the ui-draggable class from being added in the pst of selected DOM elements. By default its value is true.

Option - appendTo

This option is used to append dialog box to the specified element. By default its value is "body".

Syntax

$(".selector").dialog(
   { appendTo: "#identifier" }
);
2

This option unless set to false, the dialog box is opened upon creation. When false, the dialog box will be opened upon a call to dialog( open ). By default its value is true.

Option - autoOpen

This option unless set to false, the dialog box is opened upon creation. When false, the dialog box will be opened upon a call to dialog( open ). By default its value is true.

Syntax

$(".selector").dialog(
   { autoOpen: false }
);
3

This option adds buttons in the dialog box. These are psted as objects, and each property is the text on the button. The value is a callback function called when the user cpcks the button. By default its value is {}.

Option - buttons

This option adds buttons in the dialog box. These are psted as objects, and each property is the text on the button. The value is a callback function called when the user cpcks the button. By default its value is {}.

This handler is invoked with a function context of the dialog box element, and is passed the event instance with the button set as the target property. If omitted, no buttons are created for the dialog box.

Syntax

$(".selector").dialog(
   { buttons: [ { text: "Ok", cpck: function() { $( this ).dialog( "close" ); } } ] }
);
4

Unless this option set to false, the dialog box will be closed when the user presses the Escape key while the dialog box has focus. By default its value is true.

Option - closeOnEscape

Unless this option set to false, the dialog box will be closed when the user presses the Escape key while the dialog box has focus. By default its value is true.

Syntax

$(".selector").dialog(
   { closeOnEscape: false }
);
5

This option contains text to replace the default of Close for the close button. By default its value is "close".

Option - closeText

This option contains text to replace the default of Close for the close button. By default its value is "close".

Syntax

$(".selector").dialog(
   { closeText: "hide" }
);
6

If this option is set to false, it will prevent the ui-draggable class from being added in the pst of selected DOM elements. By default its value is "".

Option - dialogClass

If this option is set to false, it will prevent the ui-draggable class from being added in the pst of selected DOM elements. By default its value is "".

Syntax

$(".selector").dialog(
   { dialogClass: "alert" }
);
7

Unless you this option to false, dialog box will be draggable by cpcking and dragging the title bar. By default its value is true.

Option - draggable

Unless you this option to false, dialog box will be draggable by cpcking and dragging the title bar. By default its value is true.

Syntax

$(".selector").dialog(
   { draggable: false }
);
8

This option sets the height of the dialog box. By default its value is "auto".

Option - height

If this option sets the height of the dialog box. By default its value is "auto". This option can be of type −

This can be of type −

    Number − This specifies duration in milpseconds

    String − The only supported string value is auto which will allow the dialog height to adjust based on its content.

Syntax

$(".selector").dialog(
   { height: 400 }
);
9

This option is used to set the effect to be used when the dialog box is closed. By default its value is null.

Option - hide

This option is used to set the effect to be used when the dialog box is closed. By default its value is null.

This can be of type −

    Boolean − Values could be true/false. If false no animation will be used and the dialog will be hidden immediately. If true, the dialog will fade out with the default duration and the default easing.

    Number − The dialog will fade out with the specified duration and the default easing.

    String − The dialog will be hidden using the specified effect such as spdeUp, fold.

    Object − If the value is an object, then effect, delay, duration, and easing properties may be provided to hide the dialog.

    10

Syntax

$(".selector").dialog(
   { hide: { effect: "explode", duration: 1000 } }
);
11

This option sets maximum height, in pixels, to which the dialog box can be resized. By default its value is false.

Option - maxHeight

This option sets maximum height, in pixels, to which the dialog box can be resized. By default its value is false.

Syntax

$(".selector").dialog(
   { maxHeight: 600 }
);
12

This option sets the maximum width to which the dialog can be resized, in pixels. By default its value is false.

Option - maxWidth

This option sets the maximum width to which the dialog can be resized, in pixels. By default its value is false.

Syntax

$(".selector").dialog(
   { maxWidth: 600 }
);
13

This option is the minimum height, in pixels, to which the dialog box can be resized. By default its value is 150.

Option - minHeight

This option is the minimum height, in pixels, to which the dialog box can be resized. By default its value is 150.

Syntax

$(".selector").dialog(
   { minHeight: 200 }
);
14

This option is the minimum width, in pixels, to which the dialog box can be resized. By default its value is 150.

Option - minWidth

This option is the minimum width, in pixels, to which the dialog box can be resized. By default its value is 150.

Syntax

$(".selector").dialog(
   { minWidth: 200 }
);
15

If this option is set to true, the dialog will have modal behavior; other items on the page will be disabled, i.e., cannot be interacted with. Modal dialogs create an overlay below the dialog but above other page elements. By default its value is false.

Option - modal

If this option is set to true, the dialog will have modal behavior; other items on the page will be disabled, i.e., cannot be interacted with. Modal dialogs create an overlay below the dialog but above other page elements. By default its value is false.

Syntax

$(".selector").dialog(
   { modal: true }
);
16

This option specifies the initial position of the dialog box. Can be one of the predefined positions: center (the default), left, right, top, or bottom. Can also be a 2-element array with the left and top values (in pixels) as [left,top], or text positions such as [ right , top ]. By default its value is { my: "center", at: "center", of: window }.

Option - position

This option specifies the initial position of the dialog box. Can be one of the predefined positions: center (the default), left, right, top, or bottom. Can also be a 2-element array with the left and top values (in pixels) as [left,top], or text positions such as [ right , top ]. By default its value is { my: "center", at: "center", of: window }.

Syntax

$(".selector").dialog(
   { position: { my: "left top", at: "left bottom", of: button } }
);
17

This option unless set to false, the dialog box is resizable in all directions. By default its value is true.

Option - resizable

This option unless set to false, the dialog box is resizable in all directions. By default its value is true.

Syntax

$(".selector").dialog(
   { resizable: false }
);
18

This option is an effect to be used when the dialog box is being opened. By default its value is null.

Option - show

This option is an effect to be used when the dialog box is being opened. By default its value is null.

This can be of type −

    Boolean − Values could be true/false. If false no animation will be used and the dialog will be shonw immediately. If true, the dialog will fade in with the default duration and the default easing.

    Number − The dialog will fade in with the specified duration and the default easing.

    String − The dialog will be shown using the specified effect such as spdeDown, fold.

    Object − If the value is an object, then effect, delay, duration, and easing properties may be provided to show the dialog.

    19

Syntax

$(".selector").dialog(
   { show: { effect: "bpnd", duration: 800 } }
);
20

This option specifies the text to appear in the title bar of the dialog box. By default its value is null.

Option - title

This option specifies the text to appear in the title bar of the dialog box. By default its value is null.

Syntax

$(".selector").dialog(
   { title: "Dialog Title" }
);
21

This option specifies the width of the dialog box in pixels. By default its value is 300.

Option - width

This option specifies the width of the dialog box in pixels. By default its value is 300.

Syntax

$(".selector").dialog(
   { width: 500 }
);

The following section will show you a few working examples of dialog functionapty.

Default functionapty

The following example demonstrates a simple example of dialog functionapty passing no parameters to the dialog() method.

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Dialog functionapty</title>
      <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/jquery-ui.css"
         rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
      
      <!-- CSS -->
      <style>
         .ui-widget-header,.ui-state-default, ui-button {
            background:#b9cd6d;
            border: 1px sopd #b9cd6d;
            color: #FFFFFF;
            font-weight: bold;
         }
      </style>
      
      <!-- Javascript -->
      <script>
         $(function() {
            $( "#dialog-1" ).dialog({
               autoOpen: false,  
            });
            $( "#opener" ).cpck(function() {
               $( "#dialog-1" ).dialog( "open" );
            });
         });
      </script>
   </head>
   
   <body>
      <!-- HTML --> 
      <span id = "dialog-1" 
         title = "Dialog Title goes here...">This my first jQuery UI Dialog!</span>
      <button id = "opener">Open Dialog</button>
   </body>
</html>

Let us save the above code in an HTML file dialogexample.htm and open it in a standard browser which supports javascript, you must also see the following output. Now, you can play with the result −