Sortable
jQuery UI Sortable
Overview
The jQuery UI Sortable plugin makes selected elements sortable by dragging with the mouse.
All callbacks receive two arguments: The original browser event and a prepared ui object, view below for a documentation of this object (if you name your second argument 'ui'):
- ui.helper - the current helper element (most often a clone of the item)
- ui.position - current position of the helper
- ui.offset - current absolute position of the helper
- ui.item - the current dragged element
- ui.placeholder - the placeholder (if you defined one)
- ui.sender - the sortable where the item comes from (only exists if you move from one connected list to another)
Dependencies
- UI Core
- UI Widget
- UI Mouse
Example
$("#sortable").sortable();
<!DOCTYPE html> <html> <head> <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> <script> $(document).ready(function() { $("#sortable").sortable(); }); </script> </head> <body style="font-size:62.5%;"> <ul id="sortable"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <li>Item 4</li> <li>Item 5</li> </ul> </body> </html>
Options
Events
Methods
-
destroy
- Signature:
- .sortable( "destroy" )
Remove the sortable functionality completely. This will return the element back to its pre-init state.
-
disable
- Signature:
- .sortable( "disable" )
Disable the sortable.
-
enable
- Signature:
- .sortable( "enable" )
Enable the sortable.
-
option
- Signature:
- .sortable( "option" , optionName , [value] )
Get or set any sortable option. If no value is specified, will act as a getter.
-
option
- Signature:
- .sortable( "option" , options )
Set multiple sortable options at once by providing an options object.
-
widget
- Signature:
- .sortable( "widget" )
Returns the .ui-sortable element.
-
serialize
- Signature:
- .sortable( "serialize" , [options] )
Serializes the sortable's item id's into 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.
It works by default by looking at the id of each item in the format 'setname_number', and it spits out a hash like "setname[]=number&setname[]=number".
You can also give in a option hash as second argument to custom define how the function works. The possible options are: 'key' (replaces part1[] with whatever you want), 'attribute' (test another attribute than 'id') and 'expression' (use your own regexp).
If serialize returns an empty string, make sure the id attributes include an underscore. They must be in the form: "set_number" For example, a 3 element list with id attributes foo_1, foo_5, foo_2 will serialize to foo[]=1&foo[]=5&foo[]=2. You can use an underscore, equal sign or hyphen to separate the set and number. For example foo=1 or foo-1 or foo_1 all serialize to foo[]=1.
-
toArray
- Signature:
- .sortable( "toArray" )
Serializes the sortable's item id's into an array of string. If you have
<ul id="a_sortable"><br> <li id="hello">Hello</li><br> <li id="goodbye">Good bye</li><br> </ul>
and do
var result = $('#a_sortable').sortable('toArray');
then
result[0] will contain "hello" and result[1] will contain "goodbye".
-
refresh
- Signature:
- .sortable( "refresh" )
Refresh the sortable items. Custom trigger the reloading of all sortable items, causing new items to be recognized.
-
refreshPositions
- Signature:
- .sortable( "refreshPositions" )
Refresh the cached positions of the sortables' items. Calling this method refreshes the cached item positions of all sortables. This is usually done automatically by the script and slows down performance. Use wisely.
-
cancel
- Signature:
- .sortable( "cancel" )
Cancels a change in the current sortable and reverts it back to how it was before the current sort started. Useful in the stop and receive callback functions.
If the sortable item is not being moved from one connected sortable to another:
$(this).sortable('cancel');
will cancel the change.
If the sortable item is being moved from one connected sortable to another:
$(ui.sender).sortable('cancel');
will cancel the change. Useful in the 'receive' callback.
Theming
The jQuery UI Sortable plugin uses the jQuery UI CSS Framework to style its look and feel, including colors and background textures. We recommend using the ThemeRoller tool to create and download custom themes that are easy to build and maintain.
If a deeper level of customization is needed, there are widget-specific classes referenced within the jquery.ui.sortable.css stylesheet that can be modified. These classes are highlighed in bold below.
Sample markup with jQuery UI CSS Framework classes
<ul class="ui-sortable">Â Â Â <li></li>
   <li class="ui-sortable-helper"></li>
   <li class="ui-sortable-placeholder"></li>
   <li></li>
</ul>
Note: This is a sample of markup generated by the sortable plugin, not markup you should use to create a sortable. The only markup needed for that is
<ul>
   <li></li>
   <li></li>
   <li></li>
</ul>.