Monkeys love the Modal Dialog from Bootstrap, but they're getting angry because they have to write this stuff:
<div class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
What they want is more like this:
Like it? See Available Options or try more Examples below.
Provide only the message to show, and keep all other things default.
There are some workarounds for loading remote page into BootstrapDialog as message, one of those workarounds is as below.
Check out the remote.html.
Another solution of loading remote page into BootstrapDialog.
This one is more flexible and customizable, but it's also a bit more complicated to use.
Buttons that created by BootstrapDialog have some extra functions available:
$button.toggleEnable(true|false);
$button.enable(); // Equals to $button.toggleEnable(true);
$button.disable(); // Equals to $button.toggleEnable(false);
$button.toggleSpin(true|false);
$button.spin(); // Equals to $button.toggleSpin(true);
$button.stopSpin(); // Equals to $button.toggleSpin(false);
Try to press the keys that have been associated with buttons below.
The last button is disabled so nothing is going to happen when pressing its hotkey.
Please note that if there are some components that require keyboard operations in your dialog, conflicts may occur, you can try next example.
Try to avoid doing similar in your code.
A good looking stacked dialogs. Please also note the second and the third dialog are draggable.
BootstrapDialog.show(...) is just a shortcut method, if you need dialog instances, use 'new'.
In fact BootstrapDialog.show(...) will also return the created dialog instance.
This example demonstrates opening and closing a batch of dialogs at one time.
Dialog that created by BootstrapDialog will be in a container BootstrapDialog.dialogs before it's closed and destroyed, iterate BootstrapDialog.dialogs to find all dialogs that havn't been destroyed.
By default, the dialog size is 'size-normal' or BootstrapDialog.SIZE_NORMAL, but you can have a larger dialog by setting option 'size' to 'size-large' or BootstrapDialog.SIZE_LARGE.
Please note that specifying BootstrapDialog.SIZE_WIDE is equal to using css class 'modal-lg' on Bootstrap Modal.
BootstrapDialog supports displaying rich content, so you can even use a video clip as your message in the dialog.
If option 'closable' is set to false, the default closing behaviors will be disabled, but you can still close the dialog by using dialog.close().
By default, when option 'closable' is set to true, dialog can be closed by these ways: clicking outside the dialog, pressing ESC key, clicking the close icon on the right of dialog header.
If you want your dialog closes only when the close icon in dialog header was clicked, try setting both of the options 'closeByBackdrop' and 'closeByKeyboard' to false.
Setting option 'animate' to false to disable animation for the opening dialog.
If you want to disable animation globally, try to do this:
BootstrapDialog.configDefaultOptions({
animate: false
});
Lazy guys must love this.
When option 'draggable' is set to true, your dialog can be moved by dragging on its header.
If you would like to change the cursor shape of hovering on dialog header, you can try the following css lines:
This is useful for applying effects to specific dialogs.
For example if you would like to give your 'login-dialog' a smaller size, you can do this way:
This adds an 'aria-describedby' attribute to your dialog, which provides a description of the dialog for screen readers.
Please note that if you're going to use setters to configure event handlers, use dialog.onShow(function) and dialog.onHide(function).
Option 'onhide' gives you an opportunity to stop closing the dialog according to some conditions, making your 'onhide' callback returns false to stop closing the dialog.
In the following example, the dialog closes only when your most favorite fruit is 'banana' (Case insensitive).
All options shown below are optional.
To provide local messages for you needed, reset those messages below before using BootstrapDialog.
Please note that all options described below are optional, but you will have a weird dialog if you don't even give it a message to display.
Most options can be set via init options or property setters.
Option | Possible values | Description |
---|---|---|
type |
BootstrapDialog.TYPE_DEFAULT or 'type-default' BootstrapDialog.TYPE_INFO or 'type-info' BootstrapDialog.TYPE_PRIMARY or 'type-primary' (default) BootstrapDialog.TYPE_SUCCESS or 'type-success' BootstrapDialog.TYPE_WARNING or 'type-warning' BootstrapDialog.TYPE_DANGER or 'type-danger' |
Give your dialog a specific look, color scheme can be seen on Bootstrap's Button. |
size |
BootstrapDialog.SIZE_NORMAL or 'size-normal' (default) BootstrapDialog.SIZE_WIDE or 'size-wide' BootstrapDialog.SIZE_LARGE or 'size-large' |
- |
cssClass | - | Additional css classes that will be added to your dialog. |
title | String or Object(html) | - |
message | String or Object(html) | - |
buttons | array |
Examples:
BootstrapDialog.show({
title: 'Say-hello dialog',
message: 'Hello world!',
buttons: [{
id: 'btn-ok',
icon: 'glyphicon glyphicon-check',
label: 'OK',
cssClass: 'btn-primary',
data: {
js: 'btn-confirm',
'user-id': '3'
},
autospin: false,
action: function(dialogRef){
dialogRef.close();
}
}]
});
id: optional, if id is set, you can use dialogInstance.getButton(id) to get the button later. icon: optional, if set, the specified icon will be added to the button. cssClass: optional, additional css class to be added to the button. data: optional, object containing data attributes to be added to the button. autospin: optional, if it's true, after clicked the button a spinning icon appears. action: optional, if provided, the callback will be invoked after the button is clicked, and the dialog instance will be passed to the callback function. |
closable | true | false |
When set to true, you can close the dialog by:
|
spinicon |
Icon class name, for example 'glyphicon glyphicon-check'. Default value is 'glyphicon glyphicon-asterisk'. |
Specify what icon to be used as the spinning icon when button's 'autospin' is set to true. |
data | Key-value object. For example {'id' : '100'} | Data to be bound to the dialog. |
onshow | function |
If provided, it will be invoked when the dialog is popping up. |
onshown | function |
If provided, it will be invoked when the dialog is popped up. |
onhide | function |
If provided, it will be invoked when the dialog is popping down. |
onhidden | function |
If provided, it will be invoked when the dialog is popped down. |
autodestroy | true (default) | false | When it's true, all modal stuff will be removed from the DOM tree after the dialog is popped down, set it to false if you need your dialog (same instance) pups up and down again and again. |
description | String | If provided, 'aria-describedby' attribute will be added to the dialog with the description string as its value. This can improve accessibility, as the description can be read by screen readers. |
nl2br | true | false | Automatically convert line breaking character to <br /> if it's set to true, everything keeps original if it's false. |
Method | Description |
---|---|
open() | Open the dialog. Usage: dialogInstance.open() |
close() | Close the dialog. Usage: dialogInstance.close() |
getModal() | Return the raw modal, equivalent to $('<div class='modal fade'...></div>') |
getModalDialog() | Return the raw modal dialog. |
getModalContent() | Return the raw modal content. |
getModalHeader() | Return the raw modal header. |
getModalBody() | Return the raw modal body. |
getModalFooter() | Return the raw modal footer. |
getData(key) | Get data entry according to the given key, returns null if no data entry found. |
setData(key, value) | Bind data entry to dialog instance, value can be any types that javascript supports. |
enableButtons(boolean) | Disable all buttons in dialog footer when it's false, enable all when it's true. |
setClosable(boolean) | When set to true (default), dialog can be closed by clicking close icon in dialog header, or by clicking outside the dialog, or, ESC key is pressed. |
realize() | Calling dialog.open() will automatically get this method called first, but if you want to do something on your dialog before it's shown, you can manually call dialog.realize() before calling dialog.open(). |