jQueryUI Sortable

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:

  1. $(selector, context).sortable (options) Method
  2. $(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:

<!DOCTYPE html>  
<head>  
   <title>jQuery UI Sortable - Example</title>  
   <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
   <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
   <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
   <style>  
      #sort-1 { list-style-type: none; margin: 0;   
         padding: 0; width: 200px; }  
      #sort-1 li { margin: 0 5px 5px 5px; padding: 0.4em;   
         padding-left: 1.5em; font-size: 18px; height: 20px; }  
      .default {  
         background: crimson;  
         border: 2px solid brown;  
         color: white;  
      }  
   </style>  
   <script>  
      $(function() {  
         $( "#sort-1" ).sortable();  
      });  
   </script>  
</head>  
<body>  
<h1>List to re-order</h1>  
   <ul id="sort-1">  
      <li class="default">First</li>  
      <li class="default">Second</li>  
      <li class="default">Third</li>  
      <li class="default">Fourth</li>  
      <li class="default">Fifth</li>  
      <li class="default">Sixth</li>  
      <li class="default">Seventh</li>  
   </ul>  
</body>  
</html>

Output 1:

Output 2:

Explanation:

In the above example, we are displaying the sortable functionality. Here, we are passing no parameters to the sortable() method.

Example 2:

<!DOCTYPE html>  
<head>  
   <title>jQuery UI Sortable - Example</title>  
   <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
   <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
   <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
   <style>  
      #sort-1, #sort-2 { list-style-type: none; margin: 0;   
         padding: 0; width: 200px; }  
      #sort-1 li, #sort-2 li { margin: 0 5px 5px 5px; padding: 0.4em;   
         padding-left: 1.5em; font-size: 18px; height: 20px; }  
      .default {  
         background: crimson;  
         border: 2px solid brown;  
         color: white;
      }  
   </style>  
   <script>  
      $(function() {  
         $( "#sort-1" ).sortable({  
            delay:1000  
         });  
         $( "#sort-2" ).sortable({  
            distance:150  
         });  
      });  
   </script>  
</head>  
<body>  
   <h3>Delay by 1000ms</h3>  
   <ul id="sort-1">  
      <li class="default">First</li>  
      <li class="default">Second</li>  
      <li class="default">Third</li>  
      <li class="default">Fourth</li>   
   </ul>  
   <h3>Distance Delay by 150px</h3>  
   <ul id="sort-2">  
      <li class="default">Fifth</li>  
      <li class="default">Sixth</li>  
      <li class="default">Seventh</li> 
      <li class="default">Eighth</li>  
   </ul>  
</body>  
</html>

Output 1:

Output 2:

Explanation:

In the above example, we are displaying the usage of the delay and distance in jQuery UI sorting.

Example 3:

<!DOCTYPE html>  
<head>  
   <title>jQuery UI Sortable - Example</title>  
   <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
   <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
   <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
   <style>  
      #sort-1 { list-style-type: none; margin: 0;   
         padding: 0; width: 200px; }  
      #sort-1 li { margin: 0 5px 5px 5px; padding: 0.4em;   
         padding-left: 1.5em; font-size: 18px; height: 20px; }  
      .highlight {  
         border: 2px solid crimson;  
         font-weight: bold;  
         font-size: 40px;  
         background-color: gray;  
      }  
      .default {  
         background: crimson;  
         border: 2px solid brown;  
         color: white;  
      }  
   </style>  
   <script>  
      $(function() {  
         $( "#sort-1" ).sortable({  
          placeholder: "highlight"  
         });  
      });  
   </script>  
</head>  
<body>  
<h1>List to re-order</h1>  
   <ul id="sort-1">  
      <li class="default">First</li>  
      <li class="default">Second</li>  
      <li class="default">Third</li>  
      <li class="default">Fourth</li>  
      <li class="default">Fifth</li>  
      <li class="default">Sixth</li>  
      <li class="default">Seventh</li>  
   </ul>  
</body>  
</html>

Output 1:

Output 2:

Output 3:

Explanation:

In the above example, we are displaying the use of the Placeholder in the sort() function of jQuery UI.

Example 4:

<!DOCTYPE html>  
<head>  
   <title>jQuery UI Sortable - Example</title>  
   <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
   <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
   <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
   <style>  
      #sort-1, #sort-2,#sort-3 {   
         list-style-type: none; margin: 0;   
         padding: 0; width: 200px; float:left}  
      #sort-1 li, #sort-2 li,#sort-3 li {   
         margin: 0 5px 5px 5px; padding: 0.4em;   
         padding-left: 1.5em; font-size: 18px; height: 20px; }  
      .default {  
         background: crimson;  
         border: 2px solid brown;  
         color: white;  
      }  
   </style>  
   <script>  
      $(function() {  
         $( "#sort-1, #sort-2" ).sortable({  
            connectWith: "#sort-1, #sort-2"  
         });  
         $( "#sort-3").sortable({  
            connectWith: "#sort-1",  
            dropOnEmpty: false  
         });  
 
      });  
   </script>  
</head>  
<body>  
   <ul id="sort-1"><h3>Group A</h3>  
      <li class="default">First</li>  
      <li class="default">Second</li>  
      <li class="default">Third</li>  
   </ul>  
   <ul id="sort-2"><h3>Group B</h3>  
      <li class="default">Fourth</li>  
      <li class="default">Fifth</li>  
      <li class="default">Sixth</li>  
   </ul>  
   <ul id="sort-3"><h3>Group C</h3>  
      <li class="default">Seventh</li> 
      <li class="default">Eighth</li> 
      <li class="default">Ninth</li> 
   </ul>  
</body>  
</html>

Output 1:

Output 2:

Explanation:

In the above example, we are specifying the way to use the connectWith and the dropOnEmpty options in the sort function of the jQueryUI.

Second Method:

To act on the sortable elements, the sortable (action, params) method is used. It can be used to prevent displacement. Here, the first argument is the “action” which represents a string. One or multiple params can also be added based on the given action.

Syntax:

$(selector, context).sortable ("action", [params]);

The popular actions used in this method are listed below:

Action Uses
cancel() To cancel the current sort operation. Within the handlers for the sort receive and sort stop events, it is very useful. No argument is accepted by this method.
destroy() To remove the sortability functionality completely, thus returning the element back to its pre-init state. No argument is accepted by this method.
disable() To disable the sortability of any sortable elements in the wrapped set. Only the sortability of the elements not removed is disabled which can be restored by calling the enable variant of this method. No argument is accepted by this method.
enable() To re-enable the sortability on any sortable elements in the wrapped set whose sortability has been disabled. It doesn’t add sortability to any non-sortable elements. No argument is accepted by this method.
option(optionName) To retrieve the value currently associated with the specified optionname. The name of the option to get is specified by the optionname.
option() To retrieve an object containing key/value pairs representing the current sortable options hash. No argument is accepted by this method.
option(optionName, value) To set the value of the sortable option associated with the specified optionname. The name of the option to set is specified by the optionname and the value to set for the option is specified by the value.
option(options) To set one or more options for the sortable. A map of option-value pairs to set is specified by the options parameter.
refresh() To refresh the list of items if necessary. It will be added to the sortable to be recognized, when called. No argument is accepted by this method.
toArray(options) To return an array of the id values of the sortable elements in sorted order. It takes options as parameter, to customize the serialization or sorted order.
serialize(options) To retrieve a serialized query string (submittable via ajax) formed from the sortable.
refreshPositions() To refresh the cached information of the sortable. It is used mostly internally. No argument is accepted by this method.
widget() To retrieve a jquery object containing the sortable element. No argument is accepted by this method.

Example 5:

<!DOCTYPE html>  
<head>  
   <title>jQuery UI Sortable - Example</title>  
   <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
   <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
   <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
   <style>  
      #sort-1{ list-style-type: none; margin: 0;   
         padding: 0; width: 200px; float:left;}  
      #sort-1 li{ margin: 0 5px 5px 5px; padding: 0.4em;   
         padding-left: 1.5em; font-size: 18px; height: 20px; }  
      .default {  
         background: crimson;  
         border: 2px solid brown;  
         color: white;  
      }  
   </style>  
   <script>  
      $(function() {  
         $('#sort-1').sortable({  
            update: function(event, ui) {  
               var productOrder = $(this).sortable('toArray').toString();  
               $("#sort-2").text (productOrder);  
            }  
         });  
      });  
   </script>  
</head>  
<body>  
   <ul id="sort-1">  
      <li id="1" class="default">First</li>  
      <li id="2" class="default">Second</li>  
      <li id="3" class="default">Third</li>  
      <li id="4" class="default">Fourth</li>  
      <li id="5" class="default">Fifth</li>  
      <li id="6" class="default">Sixth</li>  
      <li id="7" class="default">Seventh</li>  
   </ul>  
   <br>  
   <h3><span id="sort-2"></span></h3>  
</body>  
</html>

Output 1:

Output 2:

Explanation:

In the above example, we are displaying the use of the toArray (options) method. Here, the ID of each item of the list is displayed in their respective sequence of appearance. The sequence of the ID also changes, each time the items are reordered in the list.

Event Management:

Event methods are also provided by the jQueryUI sortable method. Each event method can be triggered for a particular event.

The popular event methods are listed below:

Event Method Uses
activate(event, ui) Triggered when a sort operation starts on connected sortable.
beforestop(event, ui) Triggered when the sort operation is about to end, with the helper and placeholder element reference still valid.
change(event, ui) Triggered when the sorted element changes position within the dom.
create (event, ui) Triggered when the sortable is created.
deactivate (event, ui) Triggered when a connected sort stops, propagated to the connected sortable.
out(event, ui) Triggered when the sort item is moved away from a connected list.
over(event, ui) Triggered when a sort item moves into a connected list.
receive(event, ui) Triggered when a connected list has received a sort item from another list.
remove(event, ui) Triggered when the sort item is removed from a connected list and is dragged into another.
sort(event,ui) Repeatedly triggered for mousemove events during a sort operation.
start(event,ui) Triggered when a sort operation starts.
stop(event,ui) Triggered when a sort operation has concluded.
update(event, ui) Triggered when a sort operation stops and the position of the item has been changed.

Example 6:

<!DOCTYPE html>  
<head>  
   <title>jQuery UI Sortable - Example</title>  
   <link href="https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
   <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
   <script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
   <style>  
      #sort-1, #sort-2 {   
         list-style-type: none; margin: 0;   
         padding: 0; width: 200px; float:left}  
      #sort-1 li, #sort-2 li {   
         margin: 0 5px 5px 5px; padding: 0.4em;   
         padding-left: 1.5em; font-size: 18px; height: 20px; }  
      .highlight {  
         border: 2px solid crimson;  
         font-weight: bold;  
         font-size: 40px;  
         background-color: gray;  
      }  
      .default {  
         background: crimson;  
         border: 2px solid gray;  
         color: white;  
         }  
      .wrap{  
         display: table-row-group;  
         }  
      .wrap1{  
         float:left;  
         width: 200px;  
      }  
   </style>  
   <script>  
      $(function() {  
         $( "#sort-1" ).sortable({  
            start: function (event, ui) {  
               $("span#result").html ($("span#result").html ()   
                  + "<b>START</b><br>");  
            },  
            receive : function (event, ui)  
            {  
               $("span#result").html ($("span#result").html ()   
                  + ", receive");  
            },  
            stop: function (event, ui) {  
               $("span#result").html ($("span#result").html ()   
                  + "<b>STOP</b><br>");  
            }  
         });  
         $( "#sort-2" ).sortable({  
            connectWith : "#sort-1, #sort-2"  
         });  
      });  
   </script>  
</head>  
<body>  
   <div class="wrap">  
      <div class="wrap1">   
         <h3>Group A</h3>  
         <ul id="sort-1">  
      <li id="1" class="default">First</li>  
      <li id="2" class="default">Second</li>  
      <li id="3" class="default">Third</li>  
      <li id="4" class="default">Fourth</li>  
      <li id="5" class="default">Fifth</li>  
         </ul>  
      </div>  
      <div class="wrap1">  
         <h3>ID</h3>   
         <ul id="sort-2">  
            <li class="default">#1</li>  
            <li class="default">#2</li>  
            <li class="default">#3</li>  
            <li class="default">#4</li>
            <li class="default">#5</li>  
         </ul>  
      </div>  
   </div>  
   <hr />  
   <span id=result></span>  
</body>  
</html>

Output 1:

Output 2:

Explanation:

In the above example, we are displaying the use of the event receive, start and stop during drop functionality. The start event is triggered every time a sort operation starts and thus a string “START” is displayed on the screen as the result of the event triggering. Similarly, the stop event is triggered every time a sort operation reach its conclusion and thus a string “STOP” is displayed on the screen as the result of the event triggering.

Please Share