English 中文(简体)
JqueryUI - Sortable
  • 时间:2024-11-03

JqueryUI - Sortable


Previous Page Next Page  

jQueryUI provides sortable() method to reorder elements in pst or grid using the mouse. This method performs sortabipty action based upon an operation string passed as the first parameter.

Syntax

The sortable () method can be used in two forms −

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

The sortable (options) method declares that an HTML element contains interchangeable elements. The options parameter is an object that specifies the behavior of the elements involved during reordering.

Syntax

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

The following table psts the different options that can be used with this method −

Sr.No. Option & Description
1

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.

Option - 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.

This can be of type −

    Selector − This indicates a selector specifying which element to append the helper to..

    jQuery − This indicates a jQuery object containing the element to append the helper to.

    Element − An element in the Document Object Model (DOM) to append the helper to.

    String − The string "parent" will cause the helper to be a sibpng of the sortable item.

Syntax

$(".selector").sortable(
   { appendTo: document.body }
);
2

This option indicates an axis of movement ("x" is horizontal, "y" is vertical). By default its value is false.

Option - axis

This option indicates an axis of movement ("x" is horizontal, "y" is vertical). By default its value is false.

Syntax

$(".selector").sortable(
   { axis: "x" }
);
3

This option is used to prevent sorting of elements by cpcking on any of the selector elements. By default its value is "input,textarea,button,select,option".

Option - cancel

This option is used to prevent sorting of elements by cpcking on any of the selector elements. By default its value is "input,textarea,button,select,option".

Syntax

$(".selector").sortable(
   { cancel: "a,button" }
);
4

This option is a Selector that identifies another sortable element that can accept items from this sortable. This allows items from one pst to be moved to other psts, 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.

Option - connectWith

This option is a Selector that identifies another sortable element that can accept items from this sortable. This allows items from one pst to be moved to other psts, 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.

Syntax

$(".selector").sortable(
   { connectWith: "#identifier" }
);
5

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 pst will be considered), a DOM element, or the string "parent" (parent element) or "window" (HTML page).

Option - containment

This option indicates an element within which the displacement takes place.

This can be of type −

    Selector − This indicates a selector. The element will be represented by a selector (only the first item in the pst will be considered)

    Element − An DOM element to use as the container.

    String − The string identifying an element to use as the container. Possible values are parent (parent element), document or window (HTML page).

Syntax

$(".selector").sortable(
   { containment: "parent" }
);
6

Specifies the cursor CSS property when the element moves. It represents the shape of the mouse pointer. By default its value is "auto".

Option - cursor

Specifies the cursor CSS property when the element moves. It represents the shape of the mouse pointer. By default its value is "auto". Possible values are −

    "crosshair" (across)

    "default" (an arrow)

    "pointer" (hand)

    "move" (two arrows cross)

    "e-resize" (expand to the right)

    "ne-resize" (expand up right)

    "nw-resize" (expand up left)

    "n-resize" (expand up)

    "se-resize" (expand down right)

    "sw-resize" (expand down left)

    "s-resize" (expand down)

    "auto" (default)

    "w-resize" (expand left)

    "text" (pointer to write text)

    "wait" (hourglass)

    "help" (help pointer)

Syntax

$(".selector").sortable(
   { cursor: "move" }
);
7

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".

Option - cursorAt

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".

Syntax

$(".selector").sortable(
   { cursorAt: { left: 5 } }
);
8

Delay, in milpseconds, 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".

Option - delay

Delay, in milpseconds, 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".

Syntax

$(".selector").sortable(
   { delay: 150 }
);
9

This option if set to true, disables the sortable functionapty. By default its value is false.

Option - disabled

This option if set to true, disables the sortable functionapty. By default its value is false.

Syntax

$(".selector").sortable(
   { disabled: true }
);
10

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".

Option - distance

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".

Syntax

$(".selector").sortable(
   { distance: 5 }
);
11

This option if set to false, then items from this sortable can t be dropped on an empty connect sortable. By default its value is true.

Option - dropOnEmpty

This option if set to false, then items from this sortable can t be dropped on an empty connect sortable. By default its value is true.

Syntax

$(".selector").sortable(
   { dropOnEmpty: false }
);
12

If this option if set to true forces the helper to have a size. By default its value is false.

Option - forceHelperSize

If this option if set to true forces the helper to have a size. By default its value is false.

Syntax

$(".selector").sortable(
   { forceHelperSize: true }
);
13

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 initiapzed. By default its value is false.

Option - 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 initiapzed. By default its value is false.

Syntax

$(".selector").sortable(
   { forcePlaceholderSize: true }
);
14

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.

Option - 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.

Syntax

$(".selector").sortable(
   { grid: [ 20, 10 ] }
);
15

If specified, restricts sort from starting unless the mousedown occurs on the specified element(s). By default its value is false.

Option - handle

If specified, restricts sort from starting unless the mousedown occurs on the specified element(s). By default its value is false.

Syntax

$(".selector").sortable(
   { handle: ".handle" }
);
16

Allows for a helper element to be used for dragging display. By default its value is original.

Option - helper

Allows for a helper element to be used for dragging display. By default its value is original. Possible values are −

    String − If set to "clone", then the element will be cloned and the clone will be dragged.

    Function − A function that will return a DOMElement to use while dragging.

Syntax

$(".selector").sortable(
   { helper: "clone" }
);
17

This option specifies which items inside the DOM element to be sorted. By default its value is > *.

Option - items

This option specifies which items inside the DOM element to be sorted. By default its value is > *

Syntax

$(".selector").sortable(
   { items: "> p" }
);
18

This option is used to define the opacity of the helper while sorting. By default its value is false.

Option - opacity

This option is used to define the opacity of the helper while sorting. By default its value is false.

Syntax

$(".selector").sortable(
   { opacity: 0.5 }
);
19

This option is used to class name that gets appped to the otherwise white space.By default its value is false.

Option - placeholder

Syntax

$(".selector").sortable(
   { addClasses: false }
);
20

This option decides whether the sortable items should revert to their new positions using a smooth animation. By default its value is false.

Option - revert

This option decides whether the sortable items should revert to their new positions using a smooth animation. By default its value is false.

Syntax

$(".selector").sortable(
   { revert: true }
);
21

This option is used to enable scrolpng. If set to true the page scrolls when coming to an edge. By default its value is true.

Option - scroll

This option is used to enable scrolpng. If set to true the page scrolls when coming to an edge. By default its value is true.

Syntax

$(".selector").sortable(
   { scroll: false }
);
22

This option indicates how many pixels the mouse must exit the visible area to cause scrolpng. By default its value is 20. This option is used only with options.scroll set to true.

Option - scrollSensitivity

This option indicates how many pixels the mouse must exit the visible area to cause scrolpng. By default its value is 20. This option is used only with options.scroll set to true.

Syntax

$(".selector").sortable(
   { scrollSensitivity: 10 }
);
23

This option indicates the scrolpng speed of the display once the scrolpng begins. By default its value is 20.

Option - scrollSpeed

This option indicates the scrolpng speed of the display once the scrolpng begins. By default its value is 20.

Syntax

$(".selector").sortable(
   { scrollSpeed: 40 }
);
24

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".

Option - 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".Possible values are −

    intersect − The item overlaps the other item by at least 50%.

    pointer − The mouse pointer overlaps the other item.

Syntax

$(".selector").sortable(
   { tolerance: "pointer" }
);
25

This option represents z-index for element/helper while being sorted. By default its value is 1000.

Option - zIndex

This option represents Z-index for element/helper while being sorted. By default its value is 1000.

Syntax

$(".selector").sortable(
   { zIndex: 9999 }
);

The following section will show you a few working examples of drag functionapty.

Default functionapty

The following example demonstrates a simple example of sortable functionapty, passing no parameters to the sortable() method.

<!DOCTYPE html>
<html>
   <head>
      <title>jQuery UI Sortable - Example</title>
      <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/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 { pst-style-type: none; margin: 0; 
            padding: 0; width: 25%; }
         #sortable-1 p { margin: 0 3px 3px 3px; padding: 0.4em; 
            padding-left: 1.5em; font-size: 17px; height: 16px; }
         .default {
            background: #cedc98;
            border: 1px sopd #DDDDDD;
            color: #333333;
         }
      </style>
      
      <script>
         $(function() {
            $( "#sortable-1" ).sortable();
         });
      </script>
   </head>
   
   <body>
      <ul id = "sortable-1">
         <p class = "default">Product 1</p>
         <p class = "default">Product 2</p>
         <p class = "default">Product 3</p>
         <p class = "default">Product 4</p>
         <p class = "default">Product 5</p>
         <p class = "default">Product 6</p>
         <p class = "default">Product 7</p>
      </ul>
   </body>
</html>

Let us save the above code in an HTML file sortexample.htm and open it in a standard browser which supports javascript, you should see the following output. Now, you can play with the result −