jQuery UI API - Sortable Widget
Category
Usage
Description:Use the mouse to adjust the order of elements in a list or grid.
Added in version:1.0
Dependencies:
Notes:The jQuery UI Sortable plugin makes selected elements sortable by dragging with the mouse.
Notes:In order to sort table rows,tbodymust be sortable, nottable。
Quick Navigation
| Options | Methods | Events |
|---|---|---|
| Options | Type | Description | Default value |
|---|---|---|---|
| appendTo | jQuery or Element or Selector or String | When dragging, where the helper moved by the mouse should be appended to (for example, to resolve overlap/zIndex issues). Supports multiple types:
Code examples: Initialize the sortable with the specified
$( ".selector" ).sortable({ appendTo: document.body });
After initialization, get or set the // getter var appendTo = $( ".selector" ).sortable( "option", "appendTo" ); // setter $( ".selector" ).sortable( "option", "appendTo", document.body ); |
"parent" |
| axis | String | If defined, items can be dragged only horizontally or vertically. Possible values:"x", "y"。Code examples: Initialize the sortable with the specified
$( ".selector" ).sortable({ axis: "x" });
After initialization, get or set the // getter var axis = $( ".selector" ).sortable( "option", "axis" ); // setter $( ".selector" ).sortable( "option", "axis", "x" ); |
false |
| cancel | Selector | Prevents sorting from starting on elements matching the selector. Code examples: Initialize the sortable with the specified
$( ".selector" ).sortable({ cancel: "a,button" });
After initialization, get or set the // getter var cancel = $( ".selector" ).sortable( "option", "cancel" ); // setter $( ".selector" ).sortable( "option", "cancel", "a,button" ); |
"input, textarea, button, select, option" |
| connectWith | Selector | Selector of other sortable elements that the items in this list should be connected to. This is a one-way relationship, and if you want items to be connected in both directions, you must set theconnectWithoption on both sortable elements.Code examples: Initialize the sortable with the specified
$( ".selector" ).sortable({ connectWith: "#shopping-cart" });
After initialization, get or set the // getter var connectWith = $( ".selector" ).sortable( "option", "connectWith" ); // setter $( ".selector" ).sortable( "option", "connectWith", "#shopping-cart" ); |
false |
| containment | Element or Selector or String | Defines the bounds in which the sortable items are constrained while dragging. Note: The element specified for containment must have a calculated width and height (although it does not need to be explicit). For example, if you have Supports multiple types:
Code examples: Initialize the sortable with the specified
$( ".selector" ).sortable({ containment: "parent" });
After initialization, get or set the // getter var containment = $( ".selector" ).sortable( "option", "containment" ); // setter $( ".selector" ).sortable( "option", "containment", "parent" ); |
false |
| cursor | String | Defines the cursor that is displayed when sorting. Code examples: Initialize the sortable with the specified
$( ".selector" ).sortable({ cursor: "move" });
After initialization, get or set the // getter var cursor = $( ".selector" ).sortable( "option", "cursor" ); // setter $( ".selector" ).sortable( "option", "cursor", "move" ); |
"auto" |
| cursorAt | Object | Moves the sorted element or helper so that the cursor always appears to drag from the same position. The coordinates can be given as a hash using one or two keys:{ top, left, right, bottom }。Code examples: Initialize the sortable with the specified
$( ".selector" ).sortable({ cursorAt: { left: 5 } });
After initialization, get or set the
// getter
var cursorAt = $( ".selector" ).sortable( "option", "cursorAt" );
// setter
$( ".selector" ).sortable( "option", "cursorAt", { left: 5 } );
|
false |
| delay | Integer | Time in milliseconds after mousedown until sorting starts. This option can prevent unwanted dragging when clicking on an element. Code examples: Initialize the sortable with the specified
$( ".selector" ).sortable({ delay: 150 });
After initialization, get or set the // getter var delay = $( ".selector" ).sortable( "option", "delay" ); // setter $( ".selector" ).sortable( "option", "delay", 150 ); |
0 |
| disabled | Boolean | If set totrue, disables the sortable.Code examples: Initialize the sortable with the specified
$( ".selector" ).sortable({ disabled: true });
After initialization, get or set the // getter var disabled = $( ".selector" ).sortable( "option", "disabled" ); // setter $( ".selector" ).sortable( "option", "disabled", true ); |
false |
| distance | Number | Distance in pixels after mousedown the mouse must move before sorting starts. If specified, sorting will only start when the mouse is dragged beyond the specified distance. This option can be used to allow clicks on elements within a handle. Code examples: Initialize the sortable with the specified
$( ".selector" ).sortable({ distance: 5 });
After initialization, get or set the // getter var distance = $( ".selector" ).sortable( "option", "distance" ); // setter $( ".selector" ).sortable( "option", "distance", 5 ); |
1 |
| dropOnEmpty | Boolean | If set tofalse, items from this sortable cannot be dropped on an empty connected sortable (see theconnectWithoption).Code examples: Initialize the sortable with the specified
$( ".selector" ).sortable({ dropOnEmpty: false });
After initialization, get or set the // getter var dropOnEmpty = $( ".selector" ).sortable( "option", "dropOnEmpty" ); // setter $( ".selector" ).sortable( "option", "dropOnEmpty", false ); |
true |
| forceHelperSize | Boolean | If set totrue, forces the helper to have a size.Code examples: Initialize the sortable with the specified
$( ".selector" ).sortable({ forceHelperSize: true });
After initialization, get or set the // getter var forceHelperSize = $( ".selector" ).sortable( "option", "forceHelperSize" ); // setter $( ".selector" ).sortable( "option", "forceHelperSize", true ); |
false |
| forcePlaceholderSize | Boolean | If set totrue, forces the placeholder to have a size.Code examples: Initialize the sortable with the specified
$( ".selector" ).sortable({ forcePlaceholderSize: true });
After initialization, get or set the // getter var forcePlaceholderSize = $( ".selector" ).sortable( "option", "forcePlaceholderSize" ); // setter $( ".selector" ).sortable( "option", "forcePlaceholderSize", true ); |
false |
| grid | Array | Snaps the sorted element or helper to a grid, every x and y pixels. The array must be in the form[ x, y ]。Code examples: Initialize the sortable with the specified
$( ".selector" ).sortable({ grid: [ 20, 10 ] });
After initialization, get or set the // getter var grid = $( ".selector" ).sortable( "option", "grid" ); // setter $( ".selector" ).sortable( "option", "grid", [ 20, 10 ] ); |
false |
| handle | Selector or Element | If specified, restricts sorting start to the specified element. Code examples: Initialize the sortable with the specified
$( ".selector" ).sortable({ handle: ".handle" });
After initialization, get or set the // getter var handle = $( ".selector" ).sortable( "option", "handle" ); // setter $( ".selector" ).sortable( "option", "handle", ".handle" ); |
false |
| helper | String or Function() | Allows a helper element to be used for drag display. Supports multiple types:
Code examples: Initialize the sortable with the specified
$( ".selector" ).sortable({ helper: "clone" });
After initialization, get or set the // getter var helper = $( ".selector" ).sortable( "option", "helper" ); // setter $( ".selector" ).sortable( "option", "helper", "clone" ); |
"original" |
| items | Selector | Which items within the element should be sortable. Code examples: Initialize the sortable with the specified
$( ".selector" ).sortable({ items: "> li" });
After initialization, get or set the // getter var items = $( ".selector" ).sortable( "option", "items" ); // setter $( ".selector" ).sortable( "option", "items", "> li" ); |
"> *" |
| opacity | Number | The opacity of the helper when sorting. From0.01to1。Code examples: Initialize the sortable with the specified
$( ".selector" ).sortable({ opacity: 0.5 });
After initialization, get or set the // getter var opacity = $( ".selector" ).sortable( "option", "opacity" ); // setter $( ".selector" ).sortable( "option", "opacity", 0.5 ); |
false |
| placeholder | String | The class name to apply, otherwise white blank. Code example: Initialize with the specified
$( ".selector" ).sortable({ placeholder: "sortable-placeholder" });
After initialization, get or set // getter var placeholder = $( ".selector" ).sortable( "option", "placeholder" ); // setter $( ".selector" ).sortable( "option", "placeholder", "sortable-placeholder" ); |
false |
| revert | Boolean or Number | Whether sortable items use a smooth animation to return to their new position. Supports multiple types:
Code example: Initialize with the specified
$( ".selector" ).sortable({ revert: true });
After initialization, get or set // getter var revert = $( ".selector" ).sortable( "option", "revert" ); // setter $( ".selector" ).sortable( "option", "revert", true ); |
false |
| scroll | Boolean | If set totrue, the page will scroll when the edge is reached.Code example: Initialize with the specified
$( ".selector" ).sortable({ scroll: false });
After initialization, get or set // getter var scroll = $( ".selector" ).sortable( "option", "scroll" ); // setter $( ".selector" ).sortable( "option", "scroll", false ); |
true |
| scrollSensitivity | Number | Defines how far from the edge the mouse must be for scrolling to start. Code example: Initialize with the specified
$( ".selector" ).sortable({ scrollSensitivity: 10 });
After initialization, get or set // getter var scrollSensitivity = $( ".selector" ).sortable( "option", "scrollSensitivity" ); // setter $( ".selector" ).sortable( "option", "scrollSensitivity", 10 ); |
20 |
| scrollSpeed | Number | When the mouse pointer is withinscrollSensitivitydistance, the speed at which the window scrolls. Ifscrolloption isfalsethen it is ignored.Code example: Initialize with the specified
$( ".selector" ).sortable({ scrollSpeed: 40 });
After initialization, get or set // getter var scrollSpeed = $( ".selector" ).sortable( "option", "scrollSpeed" ); // setter $( ".selector" ).sortable( "option", "scrollSpeed", 40 ); |
20 |
| tolerance | String | Specifies the mode used to test whether a moving item is overlapping another item. Possible values:
Code example: Initialize with the specified
$( ".selector" ).sortable({ tolerance: "pointer" });
After initialization, get or set // getter var tolerance = $( ".selector" ).sortable( "option", "tolerance" ); // setter $( ".selector" ).sortable( "option", "tolerance", "pointer" ); |
"intersect" |
| zIndex | Integer | The Z-index of the element/helper while being sorted. Code example: Initialize with the specified
$( ".selector" ).sortable({ zIndex: 9999 });
After initialization, get or set // getter var zIndex = $( ".selector" ).sortable( "option", "zIndex" ); // setter $( ".selector" ).sortable( "option", "zIndex", 9999 ); |
1000 |
| Methods | Returns | Description |
|---|---|---|
| cancel() | jQuery (plugin only) | Cancel a change in the current sortable during the current sort and revert to the previous state. Useful in the stop and receive callbacks.
Code example: Calling the cancel method: $( ".selector" ).sortable( "cancel" ); |
| destroy() | jQuery (plugin only) | Completely remove the sortable functionality. This returns the element to its pre-init state.
Code example: Calling the destroy method: $( ".selector" ).sortable( "destroy" ); |
| disable() | jQuery (plugin only) | Disable the sortable.
Code example: Calling the disable method: $( ".selector" ).sortable( "disable" ); |
| enable() | jQuery (plugin only) | Enable the sortable.
Code example: Calling the enable method: $( ".selector" ).sortable( "enable" ); |
| option( optionName ) | Object | Get the value currently associated with the specifiedoptionName.
Code example: Calling this method: var isDisabled = $( ".selector" ).sortable( "option", "disabled" ); |
| option() | PlainObject | Get an object containing key/value pairs that represent the current draggable option hash.
Code example: Calling this method: var options = $( ".selector" ).sortable( "option" ); |
| option( optionName, value ) | jQuery (plugin only) | Set the sortable option value associated with the specifiedoptionName.
Code example: Calling this method: $( ".selector" ).sortable( "option", "disabled", true ); |
| option( options ) | jQuery (plugin only) | Set one or more options for the sortable.
Code example: Calling this method:
$( ".selector" ).sortable( "option", { disabled: true } );
|
| refresh() | jQuery (plugin only) | Refresh the sortable items. Triggers a reload of all sortable items, causing new items to be recognized.
Code example: Calling the refresh method: $( ".selector" ).sortable( "refresh" ); |
| refreshPositions() | jQuery (plugin only) | Refresh the cached positions of the sortable items. Calling this method refreshes the cached positions of all sortable items.
Code example: Calling the refreshPositions method: $( ".selector" ).sortable( "refreshPositions" ); |
| serialize( options ) | String | Serialize the sortable itemsidinto a form/ajax submittable string. Calling this method produces a hash that can be appended to any url to easily submit a new item order back to the server.By default, it works via each item's Note: If serialization returns an empty string, make sure
Code example: Calling the serialize method:
var sorted = $( ".selector" ).sortable( "serialize", { key: "sort" } );
|
| toArray( options ) | Array | Serialize the sortable itemsidinto an array of strings.
Code example: Calling the toArray method: var sortedIDs = $( ".selector" ).sortable( "toArray" ); |
| widget() | jQuery | Returns ajQueryobject containing the sortable elements.
Code example: Calling the widget method: var widget = $( ".selector" ).sortable( "widget" ); |
| Events | Type | Description |
|---|---|---|
| activate( event, ui ) | sortactivate | This event is triggered when using connected lists, each connected list receives it at the start of the drag.
Code example: Initialize the sortable with the specified activate callback:
$( ".selector" ).sortable({
activate: function( event, ui ) {}
});
Bind an event listener to the sortactivate event:
$( ".selector" ).on( "sortactivate", function( event, ui ) {} );
|
| beforeStop( event, ui ) | sortbeforestop | This event is triggered when sorting stops, except when the placeholder/helper is still available.
Code example: Initialize a sortable with the specified beforeStop callback:
$( ".selector" ).sortable({
beforeStop: function( event, ui ) {}
});
Bind an event listener to the sortbeforestop event:
$( ".selector" ).on( "sortbeforestop", function( event, ui ) {} );
|
| change( event, ui ) | sortchange | Triggered during sorting, except when the DOM position changes.
Code example: Initialize a sortable with the specified change callback:
$( ".selector" ).sortable({
change: function( event, ui ) {}
});
Bind an event listener to the sortchange event:
$( ".selector" ).on( "sortchange", function( event, ui ) {} );
|
| create( event, ui ) | sortcreate | Triggered when the droppable is created.
Note: Code example: Initialize a sortable with the specified create callback:
$( ".selector" ).sortable({
create: function( event, ui ) {}
});
Bind an event listener to the sortcreate event:
$( ".selector" ).on( "sortcreate", function( event, ui ) {} );
|
| deactivate( event, ui ) | sortdeactivate | Triggered when sorting stops, and this event propagates to all possible connected lists.
Code example: Initialize a sortable with the specified deactivate callback:
$( ".selector" ).sortable({
deactivate: function( event, ui ) {}
});
Bind an event listener to the sortdeactivate event:
$( ".selector" ).on( "sortdeactivate", function( event, ui ) {} );
|
| out( event, ui ) | sortout | Triggered when a sortable item is removed from a sortable list. Note: This event is also triggered when a sortable item is canceled.
Code example: Initialize a sortable with the specified out callback:
$( ".selector" ).sortable({
out: function( event, ui ) {}
});
Bind an event listener to the sortout event:
$( ".selector" ).on( "sortout", function( event, ui ) {} );
|
| over( event, ui ) | sortover | Triggered when a sortable item moves to a sortable list.
Code example: Initialize a sortable with the specified over callback:
$( ".selector" ).sortable({
out: function( event, ui ) {}
});
Bind an event listener to the sortover event:
$( ".selector" ).on( "sortout", function( event, ui ) {} );
|
| receive( event, ui ) | sortreceive | Triggered when an item from a connected sortable list is placed into another list. The latter is the event target.
Code example: Initialize a sortable with the specified receive callback:
$( ".selector" ).sortable({
receive: function( event, ui ) {}
});
Bind an event listener to the sortreceive event:
$( ".selector" ).on( "sortreceive", function( event, ui ) {} );
|
| remove( event, ui ) | sortremove | Triggered when an item from a connected sortable list is placed into another list. The former is the event target.
Code example: Initialize a sortable with the specified remove callback:
$( ".selector" ).sortable({
remove: function( event, ui ) {}
});
Bind an event listener to the sortremove event:
$( ".selector" ).on( "sortremove", function( event, ui ) {} );
|
| sort( event, ui ) | sort | This event is triggered during sorting.
Code example: Initialize a sortable with the specified sort callback:
$( ".selector" ).sortable({
sort: function( event, ui ) {}
});
Bind an event listener to the sort event:
$( ".selector" ).on( "sort", function( event, ui ) {} );
|
| start( event, ui ) | sortstart | This event is triggered when sorting starts.
Code example: Initialize a sortable with the specified start callback:
$( ".selector" ).sortable({
start: function( event, ui ) {}
});
Bind an event listener to the sortstart event:
$( ".selector" ).on( "sortstart", function( event, ui ) {} );
|
| stop( event, ui ) | sortstop | This event is triggered when sorting stops.
Code example: Initialize a sortable with the specified stop callback:
$( ".selector" ).sortable({
stop: function( event, ui ) {}
});
Bind an event listener to the sortstop event:
$( ".selector" ).on( "sortstop", function( event, ui ) {} );
|
| update( event, ui ) | sortupdate | This event is triggered when the user stops sorting and the DOM position has changed.
Code example: Initialize a sortable with the specified update callback:
$( ".selector" ).sortable({
update: function( event, ui ) {}
});
Bind an event listener to the sortupdate event:
$( ".selector" ).on( "sortupdate", function( event, ui ) {} );
|
Examples
A simple jQuery UI Sortable Widget.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>可排序小部件(Sortable Widget)演示</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
</head>
<body>
<ul id="sortable">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
<script>$("#sortable").sortable();</script>
</body>
</html>
Other Extensions