jQuery UI API - Selectable Widget
Category
Usage
**Description:** Select single elements or groups of elements using the mouse.
**Version added:** 1.0
Dependencies:
**Note:** The jQuery UI Selectable plugin allows elements to be selected by dragging the mouse (sometimes referred to as a lasso). Multiple (non-contiguous) elements can be selected by clicking or dragging while holding the ctrl/meta key.
**Additional notes:** This widget requires some functional CSS, otherwise it will not work. If you create a custom theme, use the widget-specific CSS file as a starting point.
Quick navigation
| Option | Method | Events |
|---|---|---|
| Option | Type | Description | Default value |
|---|---|---|---|
| appendTo | Selector | The element to which the selection helper (lasso) is added. |
代码实例:
初始化带有指定 appendTo 选项的 draggable:
$( ".selector" ).selectable({ appendTo: "#someElem" });
After initialization, get or set appendTo Option:
// getter var appendTo = $( ".selector" ).selectable( "option", "appendTo" );| "body"| onSelect | event | autoRefresh | Boolean | This option determines whether to update (recalculate) the position and size of each selectable at the start of each selection operation. If you have multiple items, you may want to set this option to false and manually call// setter $( ".selector" ).selectable( "option", "appendTo", "#someElem" );
refresh() method.
Code example:
Initializes with the specified autoRefresh Options of draggable:
$( ".selector" ).selectable({ autoRefresh: false });
After initialization, get or set autoRefresh Option:
// getter var autoRefresh = $( ".selector" ).selectable( "option", "autoRefresh" );| true| onSelect | event | cancel | Selector | Prevents selection from starting on elements matching the selector.// setter $( ".selector" ).selectable( "option", "autoRefresh", false );
Code example:
Initializes with the specified cancel The selectable option:
$( ".selector" ).selectable({ cancel: "a,.cancel" });
After initialization, get or set cancel Option:
// getter var cancel = $( ".selector" ).selectable( "option", "cancel" );| "input, textarea, button, select, option"| onSelect | event | delay | Number | The time in milliseconds after the mouse button is pressed until selection starts. This option can prevent unwanted selection when clicking on an element.// setter $( ".selector" ).selectable( "option", "cancel", "a,.cancel" );
Code example:
Initializes with the specified delay The selectable option:
$( ".selector" ).selectable({ delay: 150 });
After initialization, get or set delay Option:
// getter var delay = $( ".selector" ).selectable( "option", "delay" );| 0| onSelect | event | disabled | Boolean | If set to// setter $( ".selector" ).selectable( "option", "delay", 150 );
true, then the selectable is disabled.
Code example:
Initializes with the specified disabled The selectable option:
$( ".selector" ).selectable({ disabled: true });
After initialization, get or set disabled Option:
// getter var disabled = $( ".selector" ).selectable( "option", "disabled" );| false| onSelect | event | distance | Number | The distance in pixels the mouse must move after mousedown before selection starts. If this option is specified, selection starts only when the mouse is dragged beyond the specified distance. This option can prevent unwanted selection when clicking on an element.// setter $( ".selector" ).selectable( "option", "disabled", true );
Code example:
Initializes with the specified distance The selectable option:
$( ".selector" ).selectable({ distance: 30 });
After initialization, get or set distance Option:
// getter var distance = $( ".selector" ).selectable( "option", "distance" );| 0 | onSelect | event | filter | Selector | The child elements to match in order to make them selectable.// setter $( ".selector" ).selectable( "option", "distance", 30 );
Code example:
Initializes with the specified filter The selectable option:
$( ".selector" ).selectable({ filter: "li" });
After initialization, get or set filter Option:
// getter var filter = $( ".selector" ).selectable( "option", "filter" );| "*"| onSelect | event | tolerance | String | Specifies the mode used to test whether the lasso selects an item. Possible values:// setter $( ".selector" ).selectable( "option", "filter", "li" );
"fit": The lasso fully overlaps the item."touch": The lasso overlaps the item to any extent.
Code example:
Initializes with the specified tolerance The selectable option:
$( ".selector" ).selectable({ tolerance: "fit" });
After initialization, get or set tolerance Option:
// getter var tolerance = $( ".selector" ).selectable( "option", "tolerance" );| "touch" |// setter $( ".selector" ).selectable( "option", "tolerance", "fit" );
| Method | Return | Description |
|---|---|---|
| destroy() | jQuery (plugin only) | Completely removes the selectable functionality. This returns the element to its pre-initialization state. |
Code example:
Call the destroy method:
$( ".selector" ).selectable( "destroy" );| onSelect | event | disable() | jQuery (plugin only) | Disables the selectable.
- This method accepts no arguments.
Code example:
Call the disable method:
$( ".selector" ).selectable( "disable" );| onSelect | event | enable() | jQuery (plugin only) | Enables the selectable.
- This method accepts no arguments.
Code example:
Call the enable method:
$( ".selector" ).selectable( "enable" );| onSelect | event | option( optionName ) | Object | Get the value currently associated with the specified `optionName`.
- optionName
Type: String
Description: The name of the option to get.
Code example:
Call this method:
var isDisabled = $( ".selector" ).selectable( "option", "disabled" );| onSelect | event | option() | PlainObject | Gets an object containing key/value pairs that represent the current selectable options hash.
- This method accepts no arguments.
Code example:
Call this method:
var options = $( ".selector" ).selectable( "option" );| onSelect | event | option( optionName, value ) | jQuery (plugin only) | Sets the value of the selectable option associated with the specified `optionName` option.
- optionName
Type: String
Description: The name of the option to set. - value
Type: Object
Description: The value to set for the option.
Code example:
Call this method:
$( ".selector" ).selectable( "option", "disabled", true );| onSelect | event | option( options ) | jQuery (plugin only) | Sets one or more options for the selectable.
- options
Type: Object
Description: The option-value pairs to set.
Code example:
Call this method:
$( ".selector" ).selectable( "option", { disabled: true } );
| onSelect | event | refresh() | jQuery (plugin only) | Updates the position and size of each selectable element. When the `autoRefresh` option is set to false, this method can be used to manually recalculate the position and size of each selectable.
- This method accepts no arguments.
Code example:
Call the refresh method:
$( ".selector" ).selectable( "refresh" );| onSelect | event | widget() | jQuery | Returns a `jQuery` object containing the selectable element.
- This method accepts no arguments.
Code example:
Call the widget method:
var widget = $( ".selector" ).selectable( "widget" );|
| Events | Type | Description |
|---|---|---|
| create( event, ui ) | selectablecreate | Triggered when the selectable is created. |
Type: Event
Type: Object
Note:ui The object is empty; it is included here for consistency with other events.
Code example:
Initialize the selectable with the specified create callback:
$( ".selector" ).selectable({
create: function( event, ui ) {}
});
Bind an event listener to the selectablecreate event:
$( ".selector" ).on( "selectablecreate", function( event, ui ) {} );
| onSelect | event | selected( event, ui ) | selectableselected | Triggered at the end of the selection operation when an element is added to the selection.
- event
Type: Event - ui
Type: Object- selected
Type: Element
Description: The selectable item that was selected.
- selected
Code example:
Initialize the selectable with the specified selected callback:
$( ".selector" ).selectable({
selected: function( event, ui ) {}
});
Bind an event listener to the selectableselected event:
$( ".selector" ).on( "selectableselected", function( event, ui ) {} );
| onSelect | event | selecting( event, ui ) | selectableselecting | Triggered during the selection operation when an element is added to the selection.
- event
Type: Event - ui
Type: Object- selecting
Type: Element
Description: The current selectable item being selected.
- selecting
Code example:
Initialize the selectable with the specified selecting callback:
$( ".selector" ).selectable({
selecting: function( event, ui ) {}
});
Bind an event listener to the selectableselecting event:
$( ".selector" ).on( "selectableselecting", function( event, ui ) {} );
| onSelect | event | start( event, ui ) | selectablestart | Triggered at the start of the selection operation.
- event
Type: Event - ui
Type: Object
Note:ui The object is empty; it is included here for consistency with other events.
Code example:
Initialize the selectable with the specified start callback:
$( ".selector" ).selectable({
start: function( event, ui ) {}
});
Bind an event listener to the selectablestart event:
$( ".selector" ).on( "selectablestart", function( event, ui ) {} );
| onSelect | event | stop( event, ui ) | selectablestop | Triggered at the end of the selection operation.
- event
Type: Event - ui
Type: Object
Note:ui The object is empty; it is included here for consistency with other events.
Code example:
Initialize the selectable with the specified stop callback:
$( ".selector" ).selectable({
stop: function( event, ui ) {}
});
Bind an event listener to the selectablestop event:
$( ".selector" ).on( "selectablestop", function( event, ui ) {} );
| onSelect | event | unselected( event, ui ) | selectableunselected | Triggered at the end of the selection operation when an element is removed from the selection.
- event
Type: Event - ui
Type: Object- unselected
Type: Element
Description: The selectable items that are not selected.
- unselected
Code example:
Initialize the selectable with the specified unselected callback:
$( ".selector" ).selectable({
unselected: function( event, ui ) {}
});
Bind an event listener to the selectableunselected event:
$( ".selector" ).on( "selectableunselected", function( event, ui ) {} );
| onSelect | event | unselecting( event, ui ) | selectableunselecting | Triggered during the selection operation when an element is removed from the selection.
- event
Type: Event - ui
Type: Object- unselecting
Type: Element
Description: The current selectable item being unselected.
- unselecting
Code example:
Initialize the selectable with the specified unselecting callback:
$( ".selector" ).selectable({
unselecting: function( event, ui ) {}
});
Bind an event listener to the selectableunselecting event:
$( ".selector" ).on( "selectableunselecting", function( event, ui ) {} );
|
Example
A simple jQuery UI selectable widget.
可选择小部件(Selectable Widget)演示
- Item 1
- Item 2
- Item 3
- Item 4
- Item 5