To re-order the elements in the list or grid form with the use of the mouse, the jQueryUI sortable() method is used. An operation string passed as the first parameter, is what the sorting ability of this method is based on.
Syntax:
The sortable() method can be used in two forms:
- $(selector, context).sortable (options) Method
- $(selector, context).sortable (“action”, params) Method
First Method:
To specify that an HTML element contains interchangeable elements, the sortable (options) method is used. The behavior of the elements involved during the process of reordering is specified by the options parameter.
Syntax:
$(selector, context).sortable (options);
The popular options that can be used with this method are listed below:
Option | Uses |
appendto | To specify the element in which the new element created with options.helper will be inserted during the time of the move/drag. The default value is parent. |
axis | To indicate an axis of movement (“x” is horizontal, “y” is vertical). The default value is false. |
cancel | To restrict the sorting of elements by clicking on any of the selector elements. The default value is “input,textarea,button,select,option”. |
connectwith | To identify another sortable element that can accept items from this sortable. The items are thus allowed from one list to be moved to other lists, a frequent and useful user interaction. No other element is connected, if omitted as it is a one-way relationship. The default value is false. It is a selector. |
containment | To indicate an element within which the displacement takes place. The selector thus represents the element (only the first item in the list will be considered), a DOM element, or the string “parent” (parent element) or “window” (html page). |
cursor | To define the cursor CSS property when the element moves, thus specifying the shape of the mouse pointer. The default value is “auto”. |
cursorat | To set the offset of the dragging helper relative to the mouse cursor. Using a combination of one or two keys, coordinates can be given as a hash: { top, left, right, bottom }. The default value is “false”. |
delay | To define the delay in milliseconds, after which the first movement of the mouse is taken into account and the displacement may begin after that time. The default value is “0”. |
disabled | To disable the sortable functionality, when set to true. The default value is false. |
distance | To indicate the number of pixels that the mouse must be moved before the sorting starts. Sorting will not start until after mouse is dragged beyond distance, when specified. The default value is “1”. |
droponempty | The items from this sortable can’t be dropped on an empty connect sortable, when set to false. The default value is true. |
forcehelpersize | To force the helper to have a size, when set to true. The default value is false. |
forceplaceholdersize | To take into account the size of the placeholder when an item is moved, when set to true. It is only useful if the options.placeholder is initialized. The default value is false. |
grid | To indicate the number of pixels that the sorting element moves horizontally and vertically during displacement of the mouse. The default value is false. It is an array [x, y]. |
handle | To restrict the sort from starting unless the mousedown occurs on the specified element(s), when specified. The default value is false. |
helper | To be allowed for a helper element to be used for dragging display. The default value is original. |
items | To indicate which items inside the DOM element to be sorted. The default value is > *. |
opacity | To define the opacity of the helper while sorting. The default value is false. |
placeholder | To class name that gets applied to the otherwise white space. The default value is false. |
revert | To determine whether the sortable items should revert to their new positions using a smooth animation. The default value is false. |
scroll | To enable scrolling, thus the page scrolls when coming to an edge, when set to true. The default value is true. |
scrollsenstivity | To define the number of pixels the mouse must exit the visible area to cause scrolling. It is used only with options.scroll set to true. The default value is 20. |
scrollspeed | To define the scrolling speed of the display once the scrolling begins. The default value is 20. |
tolerance | To define the mode to use for testing whether the item being moved is hovering over another item. It is a string. The default value is “intersect”. |
zIndex | To specify the z-Index for element/helper while being sorted. The default value is 1000. |
Example 1:
jQuery UI Sortable - Example List to re-order
- First
- Second
- Third
- Fourth
- Fifth
- Sixth
- Seventh