How jQuery UI Works
jQuery UI contains many stateful widgets, so it differs slightly from the typical jQuery plugin usage pattern. Its installation is similar to that of most jQuery plugins; jQuery UI widgets are based on Widget Factory created, and the widget library provides a common API. So once you learn to use one, you know how to use the others. This tutorial will use the progressbar widget code example to introduce common features.
Installation
To track the state of a widget, we first introduce the widget's full lifecycle. The lifecycle begins when the widget is installed. We install a widget simply by calling the plugin on one or more elements.
$( "#elem" ).progressbar();
This initializes each element in the jQuery object; in this example, the element has id "elem". Because we call the .progressbar() method with no arguments, the widget is initialized with its default options. We can pass a set of options at installation time to override the defaults.
$( "#elem" ).progressbar({ value: 20 });
The number of options we pass at installation time can be as many or as few as we need. Any options we do not pass use their default values.
Options are part of the widget's state, so we can also set options after installation. We will cover this in the option method later on.
Method
Now that the widget is initialized, we can query its state or perform actions on it. All post-initialization actions take the form of method calls. To call a method on a widget, we pass the method name to the jQuery plugin. For example, to call a value method on the progressbar widget, we should use:
$( "#elem" ).progressbar( "value" );
If a method accepts arguments, we can pass them after the method name. For example, to pass the argument 40 Adding value method on a Progressbar Widget, we can use:
$( "#elem" ).progressbar( "value", 40 );
As with other methods in jQuery, most widget methods return the jQuery object for chaining.
$( "#elem" )
.progressbar( "value", 90 )
.addClass( "almost-done" );
Public Methods
Each widget has its own set of methods based on the functionality it provides. However, some methods are common to all widgets.
option
As we mentioned earlier, we can change options after initialization via the option method. For example, we can call the option method to change the progressbar's value to 30.
$( "#elem" ).progressbar( "option", "value", 30 );
Note that this differs from when we previously called the value method. In this example, we call the option method to change the value option to 30.
We can also get the current value of an option.
$( "#elem" ).progressbar( "option", "value" );
In addition, we can update multiple options at once by passing an object to the option method.
$( "#elem" ).progressbar( "option", {
value: 100,
disabled: true
});
You may have noticed that the option method has the same signature as getters and setters in jQuery code, like .css() and .attr(). The only difference is that you must pass the string "option" as the first argument.
disable
disable method disables the widget. In the case of the progressbar, this changes the styling to display the progress bar as disabled.
$( "#elem" ).progressbar( "disable" );
called disable method is equivalent to setting disabled option to true。
enable
enable The method is disable the opposite of the method.
$( "#elem" ).progressbar( "enable" );
called enable method is equivalent to setting disabled option to false。
destroy
If you no longer need the widget, you can destroy it and return to the original markup. This marks the end of the widget's lifecycle.
$( "#elem" ).progressbar( "destroy" );
Once you destroy a widget, you cannot call any methods on it unless you initialize the widget again. If you want to remove the element, you can do so directly via .remove(), or via .html() or .empty() ; the widget will be destroyed automatically.
widget
Some widgets generate wrapper elements, or elements that are disconnected from the original element. In the example below,widget returns the generated element. In the case of the progressbar, no wrapper is generated, so thewidget method returns the original element.
$( "#elem" ).progressbar( "widget" );
Events
All widgets have events associated with their various behaviors to notify you when the state changes. For most widgets, when an event is triggered, its name is prefixed with the widget name. For example, we can bind the progressbar's change event, which triggers whenever the value changes.
$( "#elem" ).bind( "progressbarchange", function() {
alert( "The value has changed!" );
});
Each event has a corresponding callback, exposed as an option. We can use the progressbar's change callback, which is equivalent to binding progressbarchange events.
$( "#elem" ).progressbar({
change: function() {
alert( "The value has changed!" );
}
});
Common Events
Most events are specific to individual widgets, but all widgets have a common create event. This event is triggered when the widget is created.