Saturday, September 21, 2019

jQueryUI Sortable

jQueryUI sortable() method is used to re-order elements in the list or grid form, by using the mouse. The sorting ability of this method is based on an operation string passed as the first parameter.

Syntax:

You can use the sortable () method in two forms:

  1. 1.  $(selector, context).sortable (options) Method  
  1. 2.  $(selector, context).sortable ("action", params) Method   

First Method

The sortable (options) method specifies that an HTML element contains interchangeable elements. Here the options parameter specifies the behavior of the elements involved during the process of reordering.
Syntax:
  1. $(selector, context).sortable (options);  
Following is a list of different options that can be used with this method:
OptionDescription
appendtoThis option specifies the element in which the new element created with options.helper will be inserted during the time of the move/drag. By default its value is parent.
axisThis option indicates an axis of movement ("x" is horizontal, "y" is vertical). By default its value is false.
cancelThis option is used to prevent sorting of elements by clicking on any of the selector elements. By default its value is "input,textarea,button,select,option".
connectwithThis option is a selector that identifies another sortable element that can accept items from this sortable. This allows items from one list to be moved to other lists, a frequent and useful user interaction. If omitted, no other element is connected. This is a one-way relationship. By default its value is false.
containmentThis option indicates an element within which the displacement takes place. The element will be represented by a selector (only the first item in the list will be considered), a DOM element, or the string "parent" (parent element) or "window" (html page).
cursorIt specifies the cursor CSS property when the element moves. It represents the shape of the mouse pointer. By default its value is "auto".
cursoratIt sets the offset of the dragging helper relative to the mouse cursor. Coordinates can be given as a hash using a combination of one or two keys: { top, left, right, bottom }. By default its value is "false".
delayIt specifies delay in milliseconds, after which the first movement of the mouse is taken into account. The displacement may begin after that time. By default its value is "0".
disabledThis option if set to true, disables the sortable functionality. By default its value is false.
distanceIt indicates the number of pixels that the mouse must be moved before the sorting starts. If specified, sorting will not start until after mouse is dragged beyond distance. By default its value is "1".
droponemptyIf you set this option to false, then items from this sortable can't be dropped on an empty connect sortable. By default its value is true.
forcehelpersizeIf you set this option to true, it forces the helper to have a size. By default its value is false.
forceplaceholdersizeThis option when set to true, takes into account the size of the placeholder when an item is moved. This option is only useful if options.placeholder is initialized. By default its value is false.
gridThis option is an array [x, y] indicating the number of pixels that the sorting element moves horizontally and vertically during displacement of the mouse. By default its value is false.
handleIf specified, restricts sort from starting unless the mousedown occurs on the specified element(s). By default its value is false.
helperIt is allowed for a helper element to be used for dragging display. By default its value is original.
itemsThis option specifies which items inside the DOM element to be sorted. By default its value is > *.
opacityThis option is used to define the opacity of the helper while sorting. By default its value is false.
placeholderThis option is used to class name that gets applied to the otherwise white space. By default its value is false.
revertThis option decides whether the sortable items should revert to their new positions using a smooth animation. By default its value is false.
scrollThis option is used to enable scrolling. If you set this option to true the page scrolls when coming to an edge. By default its value is true.
scrollsenstivityThis option indicates how many pixels the mouse must exit the visible area to cause scrolling. By default its value is 20. This option is used only with options.scroll set to true.
scrollspeedThis option indicates the scrolling speed of the display once the scrolling begins. By default its value is 20.
toleranceThis option is a string that specifies which mode to use for testing whether the item being moved is hovering over another item. By default its value is "intersect".
zIndexThis option represents z-Index for element/helper while being sorted. By default its value is 1000.

jQuery UI Sortable Example 1

Let's take a simple example to demonstrate the sortable functionality, passing no parameters to the sortable() method.
  1. <!DOCTYPE html>  
  2. <head>  
  3.    <title>jQuery UI Sortable - Example</title>  
  4.    <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  5.    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  6.    <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  7.    <style>  
  8.       #sortable-1 { list-style-type: none; margin: 0;   
  9.          padding: 0; width: 25%; }  
  10.       #sortable-1 li { margin: 0 3px 3px 3px; padding: 0.4em;   
  11.          padding-left: 1.5em; font-size: 17px; height: 16px; }  
  12.       .default {  
  13.          background: lightgreen;  
  14.          border: 1px solid #DDDDDD;  
  15.          color: #333333;  
  16.       }  
  17.    </style>  
  18.    <script>  
  19.       $(function() {  
  20.          $( "#sortable-1" ).sortable();  
  21.       });  
  22.    </script>  
  23. </head>  
  24. <body>  
  25. <h1> Courses Available at JavaTpoint</h1>  
  26.    <ul id="sortable-1">  
  27.       <li class="default">Java/J2EE</li>  
  28.       <li class="default">.NET</li>  
  29.       <li class="default">SQL</li>  
  30.       <li class="default">PHP</li>  
  31.       <li class="default">Android</li>  
  32.       <li class="default">C/C++</li>  
  33.       <li class="default">jQuery</li>  
  34.    </ul>  
  35. </body>  
  36. </html>  

jQueryUI Sortable example 2

How to use options delay and distance:
Let's take an example to demonstrate the usage of delay and distance in jQuery UI sorting.
  1. <!DOCTYPE html>  
  2. <head>  
  3.    <title>jQuery UI Sortable - Example</title>  
  4.    <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  5.    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  6.    <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  7.    <style>  
  8.       #sortable-2, #sortable-3 { list-style-type: none; margin: 0;   
  9.          padding: 0; width: 25%; }  
  10.       #sortable-2 li, #sortable-3 li { margin: 0 3px 3px 3px; padding: 0.4em;   
  11.          padding-left: 1.5em; font-size: 17px; height: 16px; }  
  12.       .default {  
  13.          background: lightgreen;  
  14.          border: 1px solid #DDDDDD;  
  15.          color: red;  
  16.       }  
  17.    </style>  
  18.    <script>  
  19.       $(function() {  
  20.          $( "#sortable-2" ).sortable({  
  21.             delay:200  
  22.          });  
  23.          $( "#sortable-3" ).sortable({  
  24.             distance:50  
  25.          });  
  26.       });  
  27.    </script>  
  28. </head>  
  29. <body>  
  30.    <h3>Delay by 500ms</h3>  
  31.    <ul id="sortable-2">  
  32.       <li class="default">Java</li>  
  33.       <li class="default">SQL</li>  
  34.       <li class="default">.NET</li></li>  
  35.       <li class="default">Oracle</li>  
  36.    </ul>  
  37.    <h3>Distance Delay by 50px</h3>  
  38.    <ul id="sortable-3">  
  39.       <li class="default">Java</li>  
  40.       <li class="default">SQL</li>  
  41.       <li class="default">.NET</li>  
  42.       <li class="default">Oracle</li>  
  43.    </ul>  
  44. </body>  
  45. </html>  

jQueryUI Sortable example 3

How to use a Placeholder:
Let's take an example to demonstrate the use of Placeholder in the sort() function of jQuery UI.
  1. <!DOCTYPE html>  
  2. <head>  
  3.    <title>jQuery UI Sortable - Example</title>  
  4.    <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  5.    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  6.    <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  7.    <style>  
  8.       #sortable-4 { list-style-type: none; margin: 0;   
  9.          padding: 0; width: 25%; }  
  10.       #sortable-4 li { margin: 0 3px 3px 3px; padding: 0.4em;   
  11.          padding-left: 1.5em; font-size: 17px; height: 16px; }  
  12.       .highlight {  
  13.          border: 1px solid red;  
  14.          font-weight: bold;  
  15.          font-size: 45px;  
  16.          background-color: blue;  
  17.       }  
  18.       .default {  
  19.          background: lightgreen;  
  20.          border: 1px solid #DDDDDD;  
  21.          color: red;  
  22.       }  
  23.    </style>  
  24.    <script>  
  25.       $(function() {  
  26.          $( "#sortable-4" ).sortable({  
  27.           placeholder: "highlight"  
  28.          });  
  29.       });  
  30.    </script>  
  31. </head>  
  32. <body>  
  33. <h1>Courses available at JavaTpoint</h1>  
  34.    <ul id="sortable-4">  
  35.       <li class="default">Java</li>  
  36.       <li class="default">.NET</li>  
  37.       <li class="default">Android</li>  
  38.       <li class="default">PHP</li>  
  39.       <li class="default">C/C++</li>  
  40.       <li class="default">Spring</li>  
  41.       <li class="default">Hibernate</li>  
  42.    </ul>  
  43. </body>  
  44. </html>  

jQueryUI Sortable example 4

How to use options connectWith and dropOnEmpty:
This example specifies how to use connectWith and dropOnEmpty options in the sort function of jQueryUI.
  1. <!DOCTYPE html>  
  2. <head>  
  3.    <title>jQuery UI Sortable - Example</title>  
  4.    <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  5.    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  6.    <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  7.    <style>  
  8.       #sortable-5, #sortable-6,#sortable-7 {   
  9.          list-style-type: none; margin: 0; padding: 0;  
  10.          width: 20%;float:left }  
  11.       #sortable-5 li, #sortable-6 li,#sortable-7 li {   
  12.          margin: 0 3px 3px 3px; padding: 0.4em;   
  13.       padding-left: 1.5em; font-size: 17px; height: 16px; }  
  14.       .default {  
  15.          background: lightgreen;  
  16.          border: 1px solid #DDDDDD;  
  17.          color: #333333;  
  18.       }  
  19.    </style>  
  20.    <script>  
  21.       $(function() {  
  22.          $( "#sortable-5, #sortable-6" ).sortable({  
  23.             connectWith: "#sortable-5, #sortable-6"  
  24.          });  
  25.          $( "#sortable-7").sortable({  
  26.             connectWith: "#sortable-5",  
  27.             dropOnEmpty: false  
  28.          });  
  29.   
  30.       });  
  31.    </script>  
  32. </head>  
  33. <body>  
  34.    <ul id="sortable-5"><h3>Pool A</h3>  
  35.       <li class="default">India</li>  
  36.       <li class="default">Australia</li>  
  37.       <li class="default">Bangla Desh</li>  
  38.       <li class="default">Kenya</li>  
  39.    </ul>  
  40.    <ul id="sortable-6"><h3>Pool B</h3>  
  41.       <li class="default">Pakistan</li>  
  42.       <li class="default">Sri Lanka</li>  
  43.       <li class="default">West Indies</li>  
  44.       <li class="default">Canada</li>  
  45.    </ul>  
  46.    <ul id="sortable-7"><h3>PoolC</h3>  
  47.       <li class="default">South Africa</li>  
  48.       <li class="default">England</li>  
  49.       <li class="default">Zimbambe</li>  
  50.       <li class="default">UAE</li>  
  51.    </ul>  
  52. </body>  
  53. </html>  

Second Method

The sortable (action, params) method is used to perform an action on the sortable elements such as to prevent displacement. Here action is specified as a string in the first argument and optionally you can add one or more params based on the given action.
Syntax:
  1. $(selector, context).sortable ("action", [params]);  
Following is a list of actions used in this method:
ActionDescription
cancel()This action is used to cancel the current sort operation. This is most useful within handlers for the sort receive and sort stop events. This method does not accept any arguments.
destroy()This action is used to remove the sortability functionality completely. This will return the element back to its pre-init state. This method does not accept any arguments.
disable()This action is used to disable the sortability of any sortable elements in the wrapped set. It only disables the sortability of the elements not removed and it can be restored by calling the enable variant of this method. This method does not accept any arguments.
enable()This action is used to re-enable the sortability on any sortable elements in the wrapped set whose sortability has been disabled. You should note that this method won?t add sortability to any non-sortable elements. This method does not accept any arguments.
option(optionName)This action is used to get the value currently associated with the specified optionname. Here optionname is the name of the option to get.
option()It is used to get an object containing key/value pairs representing the current sortable options hash. This method does not accept any arguments.
option(optionName, value)This action is used to set the value of the sortable option associated with the specified optionname. Here optionname is the name of the option to set and value is the value to set for the option.
option(options)It sets one or more options for the sortable. Here options is a map of option-value pairs to set.
refresh()this action is used to refresh the list of items if necessary. This method does not accept any arguments. If you call this method then it will be added to the sortable to be recognized.
toArray(options)This method is used to return an array of the id values of the sortable elements in sorted order. This method takes options as parameter, to customize the serialization or sorted order.
serialize(options)This method returns a serialized query string (submittable via ajax) formed from the sortable.
refreshPositions()This method is used mostly internally to refresh the cached information of the sortable. This method does not accept any arguments.
widget()This method returns a jquery object containing the sortable element. This method does not accept any arguments.

jQueryUI Sortable example 5

Let's take an example to demonstrate the use of above actions.
The following example shows the use of toArray (options) method:
  1. <!DOCTYPE html>  
  2. <head>  
  3.    <title>jQuery UI Sortable - Example</title>  
  4.    <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  5.    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  6.    <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  7.    <style>  
  8.       #sortable-8{ list-style-type: none; margin: 0;   
  9.          padding: 0; width: 25%; float:left;}  
  10.       #sortable-8 li{ margin: 0 3px 3px 3px; padding: 0.4em;   
  11.          padding-left: 1.5em; font-size: 17px; height: 16px; }  
  12.       .default {  
  13.          background: lightgreen;  
  14.          border: 1px solid #DDDDDD;  
  15.          color: #333333;  
  16.       }  
  17.    </style>  
  18.    <script>  
  19.       $(function() {  
  20.          $('#sortable-8').sortable({  
  21.             update: function(event, ui) {  
  22.                var productOrder = $(this).sortable('toArray').toString();  
  23.                $("#sortable-9").text (productOrder);  
  24.             }  
  25.          });  
  26.       });  
  27.    </script>  
  28. </head>  
  29. <body>  
  30.    <ul id="sortable-8">  
  31.       <li id="1" class="default">Java</li>  
  32.       <li id="2" class="default">SQL</li>  
  33.       <li id="3" class="default">PHP</li>  
  34.       <li id="4" class="default">.NET</li>  
  35.    </ul>  
  36.    <br>  
  37.    <h3><span id="sortable-9"></span></h3>  
  38. </body>  
  39. </html>  

Event Management with the jQueryUI sortable elements

jQueryUI sortable method facilitates event methods to get triggered for a particular event. Following is the list of these event methods.
event methoddescription
activate(event, ui)this event is triggered on the sortable when a sort operation starts on connected sortable.
beforestop(event, ui)this event is triggered when the sort operation is about to end, with the helper and placeholder element reference still valid.
change(event, ui)this event is triggered when the sorted element changes position within the dom.
create (event, ui)this event is triggered when the sortable is created.
deactivate (event, ui)this event is triggered when a connected sort stops, propagated to the connected sortable.
out(event, ui)this event is triggered when the sort item is moved away from a connected list.
over(event, ui)this event is triggered when a sort item moves into a connected list.
recieve(event, ui)this event is triggered when a connected list has received a sort item from another list.
remove(event, ui)this event is triggered when the sort item is removed from a connected list and is dragged into another.
sort(event,ui)this event is repeatedly triggered for mousemove events during a sort operation.
start(event,ui)this event is triggered when a sort operation starts.
stop(event,ui)this event is triggered when a sort operation has concluded.
update(event, ui)this event is triggered when a sort operation stops and the position of the item has been changed.

jQueryUI Sortable example 6

Let's take an example to demonstrate the use of event receive, start and stop during drop functionality.
  1. <!DOCTYPE html>  
  2. <head>  
  3.    <title>jQuery UI Sortable - Example</title>  
  4.    <link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">  
  5.    <script src="http://code.jquery.com/jquery-1.10.2.js"></script>  
  6.    <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>  
  7.    <style>  
  8.       #sortable-10, #sortable-11 { list-style-type: none;   
  9.          margin: 0; padding: 0; width: 80%; }  
  10.       #sortable-10 li, #sortable-11 li { margin: 0 3px 3px 3px;   
  11.          padding: 0.4em; padding-left: 1.5em;   
  12.          font-size: 17px; height: 16px; }  
  13.       .highlight {  
  14.          border: 1px solid #000000;  
  15.          font-weight: bold;  
  16.          font-size: 45px;  
  17.          background-color: lightgreen;  
  18.       }  
  19.       .default {  
  20.          background: #cedc98;  
  21.          border: 1px solid #DDDDDD;  
  22.          color: red;  
  23.          }  
  24.       .wrap{  
  25.          display: table-row-group;  
  26.          }  
  27.       .wrap1{  
  28.          float:left;  
  29.          width: 200px;  
  30.       }  
  31.    </style>  
  32.    <script>  
  33.       $(function() {  
  34.          $( "#sortable-10" ).sortable({  
  35.             start: function (event, ui) {  
  36.                $("span#result").html ($("span#result").html ()   
  37.                   + "<b>start</b><br>");  
  38.             },  
  39.             receive : function (event, ui)  
  40.             {  
  41.                $("span#result").html ($("span#result").html ()   
  42.                   + ", receive");  
  43.             },  
  44.             stop: function (event, ui) {  
  45.                $("span#result").html ($("span#result").html ()   
  46.                   + "<b>stop</b><br>");  
  47.             }  
  48.          });  
  49.          $( "#sortable-11" ).sortable({  
  50.             connectWith : "#sortable-10, #sortable-11"  
  51.          });  
  52.       });  
  53.    </script>  
  54. </head>  
  55. <body>  
  56.    <div class="wrap">  
  57.       <div class="wrap1">   
  58.          <h3>Pool A</h3>  
  59.          <ul id="sortable-10">  
  60.             <li class="default">India</li>  
  61.             <li class="default">Australia</li>  
  62.             <li class="default">Bangladesh</li>  
  63.             <li class="default">Sri Lanka</li>  
  64.          </ul>  
  65.       </div>  
  66.       <div class="wrap1">  
  67.          <h3>Popularity</h3>   
  68.          <ul id="sortable-11">  
  69.             <li class="default">89%</li>  
  70.             <li class="default">87%</li>  
  71.             <li class="default">61%</li>  
  72.             <li class="default">78%</li>  
  73.          </ul>  
  74.       </div>  
  75.    </div>  
  76.    <hr />  
  77.    <span id=result></span>  
  78. </body>  
  79. </html>  

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...