Categories
JqueryUI教程

jQuery UI Sortable

jQueryUI可排序

jQueryUI sortable()方法用于通过使用鼠标对列表或网格形式的元素进行重新排序。此方法的排序能力基于作为第一个参数传递的操作字符串。

句法:

您可以两种形式使用sortable()方法:

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

第一种方法

sortable(选项)方法指定HTML元素包含可互换的元素。在这里,options参数指定在重新排序过程中涉及的元素的行为。

句法:

$(selector, context).sortable (options);

以下是可与该方法一起使用的不同选项的列表:

Option Description
appendto This 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.
axis This option indicates an axis of movement (“x” is horizontal, “y” is vertical). By default its value is false.
cancel This 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”.
connectwith This 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.
containment This 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).
cursor It specifies the cursor CSS property when the element moves. It represents the shape of the mouse pointer. By default its value is “auto”.
cursorat It 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”.
delay It 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”.
disabled This option if set to true, disables the sortable functionality. By default its value is false.
distance It 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”.
droponempty If 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.
forcehelpersize If you set this option to true, it forces the helper to have a size. By default its value is false.
forceplaceholdersize This 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.
grid This 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.
handle If specified, restricts sort from starting unless the mousedown occurs on the specified element(s). By default its value is false.
helper It is allowed for a helper element to be used for dragging display. By default its value is original.
items This option specifies which items inside the DOM element to be sorted. By default its value is > *.
opacity This option is used to define the opacity of the helper while sorting. By default its value is false.
placeholder This option is used to class name that gets applied to the otherwise white space. By default its value is false.
revert This option decides whether the sortable items should revert to their new positions using a smooth animation. By default its value is false.
scroll This 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.
scrollsenstivity This 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.
scrollspeed This option indicates the scrolling speed of the display once the scrolling begins. By default its value is 20.
tolerance This 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”.
zIndex This option represents z-Index for element/helper while being sorted. By default its value is 1000.

jQuery UI Sortable示例1

让我们以一个简单的示例来演示可排序的功能,不将任何参数传递给sortable()方法。

<!DOCTYPE html>
<head>
   <title>jQuery UI Sortable - Example</title>
   <link href="http://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>
      #sortable-1 { list-style-type: none; margin: 0; 
         padding: 0; width: 25%; }
      #sortable-1 li { margin: 0 3px 3px 3px; padding: 0.4em; 
         padding-left: 1.5em; font-size: 17px; height: 16px; }
      .default {
         background: lightgreen;
         border: 1px solid #DDDDDD;
         color: #333333;
      }
   </style>
   <script>
      $(function() {
         $( "#sortable-1" ).sortable();
      });
   </script>
</head>
<body>
<h1> Courses Available at JavaTpoint</h1>
   <ul id="sortable-1">
      <li class="default">Java/J2EE</li>
      <li class="default">.NET</li>
      <li class="default">SQL</li>
      <li class="default">PHP</li>
      <li class="default">Android</li>
      <li class="default">C/C++</li>
      <li class="default">jQuery</li>
   </ul>
</body>
</html>

jQueryUI Sortable示例2

如何使用选项延迟和距离:

让我们以一个示例来演示jQuery UI排序中延迟和距离的用法。

<!DOCTYPE html>
<head>
   <title>jQuery UI Sortable - Example</title>
   <link href="http://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>
      #sortable-2, #sortable-3 { list-style-type: none; margin: 0; 
         padding: 0; width: 25%; }
      #sortable-2 li, #sortable-3 li { margin: 0 3px 3px 3px; padding: 0.4em; 
         padding-left: 1.5em; font-size: 17px; height: 16px; }
      .default {
         background: lightgreen;
         border: 1px solid #DDDDDD;
         color: red;
      }
   </style>
   <script>
      $(function() {
         $( "#sortable-2" ).sortable({
            delay:200
         });
         $( "#sortable-3" ).sortable({
            distance:50
         });
      });
   </script>
</head>
<body>
   <h3>Delay by 500ms</h3>
   <ul id="sortable-2">
      <li class="default">Java</li>
      <li class="default">SQL</li>
      <li class="default">.NET</li></li>
      <li class="default">Oracle</li>
   </ul>
   <h3>Distance Delay by 50px</h3>
   <ul id="sortable-3">
      <li class="default">Java</li>
      <li class="default">SQL</li>
      <li class="default">.NET</li>
      <li class="default">Oracle</li>
   </ul>
</body>
</html>

jQueryUI Sortable示例3

如何使用占位符:

让我们以一个示例来演示在jQuery UI的sort()函数中使用占位符。

<!DOCTYPE html>
<head>
   <title>jQuery UI Sortable - Example</title>
   <link href="http://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>
      #sortable-4 { list-style-type: none; margin: 0; 
         padding: 0; width: 25%; }
      #sortable-4 li { margin: 0 3px 3px 3px; padding: 0.4em; 
         padding-left: 1.5em; font-size: 17px; height: 16px; }
      .highlight {
         border: 1px solid red;
         font-weight: bold;
         font-size: 45px;
         background-color: blue;
      }
      .default {
         background: lightgreen;
         border: 1px solid #DDDDDD;
         color: red;
      }
   </style>
   <script>
      $(function() {
         $( "#sortable-4" ).sortable({
          placeholder: "highlight"
         });
      });
   </script>
</head>
<body>
<h1>Courses available at JavaTpoint</h1>
   <ul id="sortable-4">
      <li class="default">Java</li>
      <li class="default">.NET</li>
      <li class="default">Android</li>
      <li class="default">PHP</li>
      <li class="default">C/C++</li>
      <li class="default">Spring</li>
      <li class="default">Hibernate</li>
   </ul>
</body>
</html>

jQueryUI Sortable示例4

如何使用选项connectWith和dropOnEmpty:

此示例指定如何在jQueryUI的sort函数中使用connectWith和dropOnEmpty选项。

<!DOCTYPE html>
<head>
   <title>jQuery UI Sortable - Example</title>
   <link href="http://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>
      #sortable-5, #sortable-6,#sortable-7 { 
         list-style-type: none; margin: 0; padding: 0;
         width: 20%;float:left }
      #sortable-5 li, #sortable-6 li,#sortable-7 li { 
         margin: 0 3px 3px 3px; padding: 0.4em; 
      padding-left: 1.5em; font-size: 17px; height: 16px; }
      .default {
         background: lightgreen;
         border: 1px solid #DDDDDD;
         color: #333333;
      }
   </style>
   <script>
      $(function() {
         $( "#sortable-5, #sortable-6" ).sortable({
            connectWith: "#sortable-5, #sortable-6"
         });
         $( "#sortable-7").sortable({
            connectWith: "#sortable-5",
            dropOnEmpty: false
         });

      });
   </script>
</head>
<body>
   <ul id="sortable-5"><h3>Pool A</h3>
      <li class="default">India</li>
      <li class="default">Australia</li>
      <li class="default">Bangla Desh</li>
      <li class="default">Kenya</li>
   </ul>
   <ul id="sortable-6"><h3>Pool B</h3>
      <li class="default">Pakistan</li>
      <li class="default">Sri Lanka</li>
      <li class="default">West Indies</li>
      <li class="default">Canada</li>
   </ul>
   <ul id="sortable-7"><h3>PoolC</h3>
      <li class="default">South Africa</li>
      <li class="default">England</li>
      <li class="default">Zimbambe</li>
      <li class="default">UAE</li>
   </ul>
</body>
</html>

第二种方法

sortable(操作,参数)方法用于对可排序元素执行操作,例如防止位移。在这里,在第一个参数中将动作指定为字符串,并且可以选择基于给定的动作添加一个或多个参数。

句法:

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

以下是此方法中使用的操作的列表:

Action Description
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示例5

让我们以一个示例来演示上述操作的用法。

以下示例显示了toArray(options)方法的用法:

<!DOCTYPE html>
<head>
   <title>jQuery UI Sortable - Example</title>
   <link href="http://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>
      #sortable-8{ list-style-type: none; margin: 0; 
         padding: 0; width: 25%; float:left;}
      #sortable-8 li{ margin: 0 3px 3px 3px; padding: 0.4em; 
         padding-left: 1.5em; font-size: 17px; height: 16px; }
      .default {
         background: lightgreen;
         border: 1px solid #DDDDDD;
         color: #333333;
      }
   </style>
   <script>
      $(function() {
         $('#sortable-8').sortable({
            update: function(event, ui) {
               var productOrder = $(this).sortable('toArray').toString();
               $("#sortable-9").text (productOrder);
            }
         });
      });
   </script>
</head>
<body>
   <ul id="sortable-8">
      <li id="1" class="default">Java</li>
      <li id="2" class="default">SQL</li>
      <li id="3" class="default">PHP</li>
      <li id="4" class="default">.NET</li>
   </ul>
   <br>
   <h3><span id="sortable-9"></span></h3>
</body>
</html>

带有jQueryUI可排序元素的事件管理

jQueryUI可排序方法便于事件方法针对特定事件被触发。以下是这些事件方法的列表。

event method description
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示例6

让我们以一个示例来演示在放置功能期间事件接收,启动和停止的用法。

<!DOCTYPE html>
<head>
   <title>jQuery UI Sortable - Example</title>
   <link href="http://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>
      #sortable-10, #sortable-11 { list-style-type: none; 
         margin: 0; padding: 0; width: 80%; }
      #sortable-10 li, #sortable-11 li { margin: 0 3px 3px 3px; 
         padding: 0.4em; padding-left: 1.5em; 
         font-size: 17px; height: 16px; }
      .highlight {
         border: 1px solid #000000;
         font-weight: bold;
         font-size: 45px;
         background-color: lightgreen;
      }
      .default {
         background: #cedc98;
         border: 1px solid #DDDDDD;
         color: red;
         }
      .wrap{
         display: table-row-group;
         }
      .wrap1{
         float:left;
         width: 200px;
      }
   </style>
   <script>
      $(function() {
         $( "#sortable-10" ).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>");
            }
         });
         $( "#sortable-11" ).sortable({
            connectWith : "#sortable-10, #sortable-11"
         });
      });
   </script>
</head>
<body>
   <div class="wrap">
      <div class="wrap1"> 
         <h3>Pool A</h3>
         <ul id="sortable-10">
            <li class="default">India</li>
            <li class="default">Australia</li>
            <li class="default">Bangladesh</li>
            <li class="default">Sri Lanka</li>
         </ul>
      </div>
      <div class="wrap1">
         <h3>Popularity</h3> 
         <ul id="sortable-11">
            <li class="default">89%</li>
            <li class="default">87%</li>
            <li class="default">61%</li>
            <li class="default">78%</li>
         </ul>
      </div>
   </div>
   <hr />
   <span id=result></span>
</body>
</html>
error: 内容受到保护 !!