Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery UI API - Sortable Widget

jQuery UI API - Sortable Widget

Category

Interactions

Usage

**Description:** Reorder elements in a list or grid using the mouse.

**Version added:** 1.0

Dependencies:

**Note:** The jQuery UI Sortable plugin makes selected elements sortable by dragging them with the mouse.

**Note:** To sort table rows,tbody they must be sortable, not table。

Quick navigation

Option Method Events

appendTo

axis
cancel
connectWith
containment
cursor
cursorAt
delay
disabled
distance
dropOnEmpty
forceHelperSize
forcePlaceholderSize
grid
handle
helper
items
opacity
placeholder
revert
scroll
scrollSensitivity
scrollSpeed
tolerance
zIndex
|
cancel
destroy
disable
enable
option
refresh
refreshPositions
serialize
toArray
widget
|
activate
beforeStop
change
create
deactivate
out
over
receive
remove
sort
start
stop
update
|
Option Type Description Default value
appendTo jQuery, or Element, or Selector, or String Where the helper moved by the mouse is appended while dragging (for example, to work around overlap/zIndex issues).
 

支持多个类型:

  • jQuery: A jQuery object containing the element to which the helper is appended.
  • Element: The element to which the helper is appended.
  • Selector: A selector specifying which element the helper should be appended to.
  • String: String "parent" will cause the helper to become a sibling of the sortable items.
  • Code example:

    Initialize the accordion with the specified appendTo options of the sortable:

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

    After initialization, get or set the appendTo Option:

    // getter
    
    var appendTo = $( ".selector" ).sortable( "option", "appendTo" );
    
    

    // setter $( ".selector" ).sortable( "option", "appendTo", document.body );     

    | "parent"| onSelect | event | axis | String | If defined, items can only be dragged horizontally or vertically. Possible values:"x", "y"。

    Code example:

    Initialize the accordion with the specified axis options of the sortable:

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

    After initialization, get or set the axis Option:

    // getter
    
    var axis = $( ".selector" ).sortable( "option", "axis" );

    // setter $( ".selector" ).sortable( "option", "axis", "x" );     

    | false| onSelect | event | cancel | Selector | Prevents sorting from starting on elements matching the selector.

    Code example:

    Initialize the accordion with the specified cancel options of the sortable:

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

    After initialization, get or set the cancel Option:

    // getter
    
    var cancel = $( ".selector" ).sortable( "option", "cancel" );

    // setter $( ".selector" ).sortable( "option", "cancel", "a,button" );     

    | "input, textarea, button, select, option"| onSelect | event | connectWith | Selector | Selector for other sortable elements whose items should be connected with the list. This is a one-way relationship; if you want items to be connected in both directions, you must set it on both sortable elements connectWith option.

    Code example:

    Initialize the accordion with the specified connectWith options of the sortable:

    $( ".selector" ).sortable({ connectWith: "#shopping-cart" });
    
        

    After initialization, get or set the connectWith Option:

    // getter
    
    var connectWith = $( ".selector" ).sortable( "option", "connectWith" );

    // setter $( ".selector" ).sortable( "option", "connectWith", "#shopping-cart" );     

    | false| onSelect | event | containment | Element or Selector or String | Defines the boundary within which sortable items are constrained while dragging.

    Note: The element specified for containment must have a computed width and height (though it need not be set explicitly). For example, if you have float: left sortable children and specify containment: "parent", make sure the sortable/parent container has float: left , otherwise it will have height: 0, leading to undefined behavior.

    Multiple types are supported:

    Code example:

    Initialize the accordion with the specified containment options of the sortable:

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

    After initialization, get or set the containment Option:

    // getter
    
    var containment = $( ".selector" ).sortable( "option", "containment" );
    
    

    // setter $( ".selector" ).sortable( "option", "containment", "parent" );     

    | false| onSelect | event | cursor | String | Defines the cursor shown while sorting.

    Code example:

    Initialize the accordion with the specified cursor options of the sortable:

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

    After initialization, get or set the cursor Option:

    // getter
    
    var cursor = $( ".selector" ).sortable( "option", "cursor" );

    // setter $( ".selector" ).sortable( "option", "cursor", "move" );     

    | "auto"| onSelect | event | cursorAt | Object | Moves the sorted element or helper so the cursor always appears at the same position for the drag. Coordinates can be given as a hash with one or two of the following keys:{ top, left, right, bottom }。

    Code example:

    Initialize the accordion with the specified cursorAt options of the sortable:

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

    After initialization, get or set the cursorAt Option:

    // getter
    
    var cursorAt = $( ".selector" ).sortable( "option", "cursorAt" );

    // setter $( ".selector" ).sortable( "option", "cursorAt", { left: 5 } );     

    | false| onSelect | event | delay | Integer | Time in milliseconds after the mouse is pressed until sorting starts. This option can prevent unwanted dragging when clicking on an element.

    Code example:

    Initialize the accordion with the specified delay options of the sortable:

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

    After initialization, get or set the delay Option:

    // getter
    
    var delay = $( ".selector" ).sortable( "option", "delay" );

    // setter $( ".selector" ).sortable( "option", "delay", 150 );     

    | 0 | onSelect | event | disabled | Boolean | If set to true, the sortable is disabled.

    Code example:

    Initialize the accordion with the specified disabled options of the sortable:

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

    After initialization, get or set the disabled Option:

    // getter
    
    var disabled = $( ".selector" ).sortable( "option", "disabled" );

    // setter $( ".selector" ).sortable( "option", "disabled", true );     

    | false| onSelect | event | distance | Number | The distance in pixels the mouse must move before sorting begins after being pressed. If specified, sorting will only start once the mouse has been dragged beyond the given distance. This option can be used to allow clicking on elements within a handle.

    Code example:

    Initialize the accordion with the specified distance options of the sortable:

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

    After initialization, get or set the distance Option:

    // getter
    
    var distance = $( ".selector" ).sortable( "option", "distance" );

    // setter $( ".selector" ).sortable( "option", "distance", 5 );     

    | 1| onSelect | event | dropOnEmpty | Boolean | If false, items from this sortable cannot be dropped on an empty connected sortable (see connectWith option is used).

    Code example:

    Initialize the accordion with the specified dropOnEmpty options of the sortable:

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

    After initialization, get or set the dropOnEmpty Option:

    // getter
    
    var dropOnEmpty = $( ".selector" ).sortable( "option", "dropOnEmpty" );

    // setter $( ".selector" ).sortable( "option", "dropOnEmpty", false );     

    | true | onSelect | event | forceHelperSize | Boolean | If true, forces the helper to have a size.

    Code example:

    Initialize the accordion with the specified forceHelperSize options of the sortable:

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

    After initialization, get or set the forceHelperSize Option:

    // getter
    
    var forceHelperSize = $( ".selector" ).sortable( "option", "forceHelperSize" );

    // setter $( ".selector" ).sortable( "option", "forceHelperSize", true );     

    | false| onSelect | event | forcePlaceholderSize | Boolean | If true, forces the placeholder to have a size.

    Code example:

    Initialize the accordion with the specified forcePlaceholderSize options of the sortable:

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

    After initialization, get or set the forcePlaceholderSize Option:

    // getter
    
    var forcePlaceholderSize = $( ".selector" ).sortable( "option", "forcePlaceholderSize" );

    // setter $( ".selector" ).sortable( "option", "forcePlaceholderSize", true );     

    | false | onSelect | event | grid | Array | Snaps the sorted element or helper to a grid, every x and y pixels. The array form must be [ x, y ]。

    Code example:

    Initialize the accordion with the specified grid options of the sortable:

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

    After initialization, get or set the grid Option:

    // getter
    
    var grid = $( ".selector" ).sortable( "option", "grid" );

    // setter $( ".selector" ).sortable( "option", "grid", [ 20, 10 ] );     

    | false | onSelect | event | handle | Selector or Element | If specified, restricts sorting to start only on the designated elements.

    Code example:

    Initialize the accordion with the specified handle options of the sortable:

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

    After initialization, get or set the handle Option:

    // getter
    
    var handle = $( ".selector" ).sortable( "option", "handle" );

    // setter $( ".selector" ).sortable( "option", "handle", ".handle" );     

    | false| onSelect | event | helper | String or Function() | Allows a helper element to be used for drag display.

    Multiple types are supported:

    Code example:

    Initialize the accordion with the specified helper options of the sortable:

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

    After initialization, get or set the helper Option:

    // getter
    
    var helper = $( ".selector" ).sortable( "option", "helper" );
    
    

    // setter $( ".selector" ).sortable( "option", "helper", "clone" );     

    | "original"| onSelect | event | items | Selector | Specifies which items within the element should be sortable.

    Code example:

    Initialize the accordion with the specified items options of the sortable:

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

    After initialization, get or set the items Option:

    // getter
    
    var items = $( ".selector" ).sortable( "option", "items" );

    // setter $( ".selector" ).sortable( "option", "items", "> li" );     

    | "> *" | onSelect | event | opacity | Number | The opacity of the helper while sorting. From 0.01 to 1。

    Code example:

    Initialize the accordion with the specified opacity options of the sortable:

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

    After initialization, get or set the opacity Option:

    // getter
    
    var opacity = $( ".selector" ).sortable( "option", "opacity" );

    // setter $( ".selector" ).sortable( "option", "opacity", 0.5 );     

    | false| onSelect | event | placeholder | String | The class name to apply, otherwise a white space.

    Code example:

    Initialize the accordion with the specified placeholder options of the sortable:

    $( ".selector" ).sortable({ placeholder: "sortable-placeholder" });
    
        

    After initialization, get or set the placeholder Option:

    // getter
    
    var placeholder = $( ".selector" ).sortable( "option", "placeholder" );

    // setter $( ".selector" ).sortable( "option", "placeholder", "sortable-placeholder" );     

    | false | onSelect | event | revert | Boolean or Number | Whether a sortable item returns to its new position with a smooth animation.

    Multiple types are supported:

    Code example:

    Initialize the accordion with the specified revert options of the sortable:

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

    After initialization, get or set the revert Option:

    // getter
    
    var revert = $( ".selector" ).sortable( "option", "revert" );
    
    

    // setter $( ".selector" ).sortable( "option", "revert", true );     

    | false| onSelect | event | scroll | Boolean | If set to true, the page scrolls when the edge is reached.

    Code example:

    Initialize the accordion with the specified scroll options of the sortable:

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

    After initialization, get or set the scroll Option:

    // getter
    
    var scroll = $( ".selector" ).sortable( "option", "scroll" );

    // setter $( ".selector" ).sortable( "option", "scroll", false );     

    | true| onSelect | event | scrollSensitivity | Number | Defines how close to the edge the mouse must be before scrolling starts.

    Code example:

    Initialize the accordion with the specified scrollSensitivity options of the sortable:

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

    After initialization, get or set the scrollSensitivity Option:

    // getter
    
    var scrollSensitivity = $( ".selector" ).sortable( "option", "scrollSensitivity" );

    // setter $( ".selector" ).sortable( "option", "scrollSensitivity", 10 );     

    | 20 | onSelect | event | scrollSpeed | Number | The speed at which the page scrolls when the mouse pointer comes within the scrollSensitivity distance. If the scroll option is false , then it is ignored.

    Code example:

    Initialize the accordion with the specified scrollSpeed options of the sortable:

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

    After initialization, get or set the scrollSpeed Option:

    // getter
    
    var scrollSpeed = $( ".selector" ).sortable( "option", "scrollSpeed" );

    // setter $( ".selector" ).sortable( "option", "scrollSpeed", 40 );     

    | 20 | onSelect | event | tolerance | String | Specifies the mode used to test whether an item overlaps another when it is moved. Possible values:

    Code example:

    Initialize the accordion with the specified tolerance options of the sortable:

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

    After initialization, get or set the tolerance Option:

    // getter
    
    var tolerance = $( ".selector" ).sortable( "option", "tolerance" );
    
    

    // setter $( ".selector" ).sortable( "option", "tolerance", "pointer" );     

    | "intersect"| onSelect | event | zIndex | Integer | The z-index of the element/helper while being sorted.

    Code example:

    Initialize the accordion with the specified zIndex options of the sortable:

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

    After initialization, get or set the zIndex Option:

    // getter
    
    var zIndex = $( ".selector" ).sortable( "option", "zIndex" );

    // setter $( ".selector" ).sortable( "option", "zIndex", 9999 );     

    | 1000 |

    Method Return Description
    cancel() jQuery (plugin only) Cancels any change currently in progress in the sortable and restores the items to their previous state. Very useful in the stop and receive callbacks.
     
  • This method accepts no arguments.
  • Code example:

    Invoke the cancel method:

    $( ".selector" ).sortable( "cancel" );
    
        
    | onSelect | event | destroy() | jQuery (plugin only) | Completely removes the sortable functionality. This returns the element to its pre-initialization state.

    Code example:

    Invoke the destroy method:

    $( ".selector" ).sortable( "destroy" );
    
        
    | onSelect | event | disable() | jQuery (plugin only) | Disables the sortable.

    Code example:

    Invoke the disable method:

    $( ".selector" ).sortable( "disable" );
    
        
    | onSelect | event | enable() | jQuery (plugin only) | Enables the sortable.

    Code example:

    Invoke the enable method:

    $( ".selector" ).sortable( "enable" );
    
        
    | onSelect | event | option( optionName ) | Object | Gets the value currently associated with the specified `optionName`.

    Code example:

    Invoke the method:

    var isDisabled = $( ".selector" ).sortable( "option", "disabled" );
    
        
    | onSelect | event | option() | PlainObject | Gets an object containing key/value pairs representing the current draggable options hash.

    Code example:

    Invoke the method:

    var options = $( ".selector" ).sortable( "option" );
    
        
    | onSelect | event | option( optionName, value ) | jQuery (plugin only) | Sets the value of the sortable option associated with the specified `optionName`.

    Code example:

    Invoke the method:

    $( ".selector" ).sortable( "option", "disabled", true );
    
        
    | onSelect | event | option( options ) | jQuery (plugin only) | Sets one or more options for the sortable.

    Code example:

    Invoke the method:

    $( ".selector" ).sortable( "option", { disabled: true } );
    
        
    | onSelect | event | refresh() | jQuery (plugin only) | Refreshes the sortable items. Triggers a reload of all sortable items, causing new items to be recognized.

    Code example:

    Invoke the refresh method:

    $( ".selector" ).sortable( "refresh" );
    
        
    | onSelect | event | refreshPositions() | jQuery (plugin only) | Refreshes the cached positions of sortable items. Calling this method refreshes the cached item positions of all sortables.

    Code example:

    Invoke the refreshPositions method:

    $( ".selector" ).sortable( "refreshPositions" );
    
        
    | onSelect | event | serialize( options ) | String | Serializes the `id` of the sortable items into a string that can be submitted by a form/ajax. Calling this method produces a hash that can be appended to any url to easily submit a new item order back to the server.

    By default, it works through each item's id , with ids of the form "setname_number", and it produces a "setname[]=number&setname[]=number" hash.

    Note: If serialization returns an empty string, make sure the id attribute contains an underscore (_). The form must be "set_number". For example, a id attribute to "foo_1"、"foo_5"、"foo_2" 3-element list will serialize to "foo[]=1&foo[]=5&foo[]=2". You can use an underscore (_), equals sign (=), or hyphen (-) to separate the set from the number. For example,"foo=1"、"foo-1"、"foo_1" all serialize to "foo[]=1"。

    Code example:

    Invoke the serialize method:

    var sorted = $( ".selector" ).sortable( "serialize", { key: "sort" } );
    
        
    | onSelect | event | toArray( options ) | Array | Serializes the `id` of the sortable items into an array of strings.

    Code example:

    Invoke the toArray method:

    var sortedIDs = $( ".selector" ).sortable( "toArray" );
    
        
    | onSelect | event | widget() | jQuery | Returns a `jQuery` object containing the sortable element.

    Code example:

    Invoke the widget method:

    var widget = $( ".selector" ).sortable( "widget" );
    
        
    |
    Events Type Description
    activate( event, ui ) sortactivate Triggered when using connected lists; each connected list receives it when a drag starts.
     
  • event
    Type: Event
  • ui
    Type: Object
    • helper
      Type: jQuery
      Description: A jQuery object representing the helper being sorted.
    • item
      Type: jQuery
      Description: A jQuery object representing the element currently being dragged.
    • offset
      Type: Object
      Description: The current absolute position of the helper, expressed as { top, left }。
    • position
      Type: Object
      Description: The current position of the helper, expressed as { top, left }。
    • originalPosition
      Type: Object
      Description: The original position of the element, expressed as { top, left }。
    • sender
      Type: jQuery
      Description: If moved from one sortable to another, the sortable the item came from.
    • placeholder
      Type: jQuery
      Description: A jQuery object representing the element used as the placeholder.
  • Code example:

    Initialize the sortable with the specified activate callback:

    $( ".selector" ).sortable({
    
    activate: function( event, ui ) {}
    
    });
    
        

    Bind an event listener to the sortactivate event:

    $( ".selector" ).on( "sortactivate", function( event, ui ) {} );
    
        
    | onSelect | event | beforeStop( event, ui ) | sortbeforestop | Triggered when sorting stops, except when the placeholder/helper is still available.

    Code example:

    Initialize the sortable with the specified beforeStop callback:

    $( ".selector" ).sortable({
    
    beforeStop: function( event, ui ) {}
    
    });
    
        

    Bind an event listener to the sortbeforestop event:

    $( ".selector" ).on( "sortbeforestop", function( event, ui ) {} );
    
        
    | onSelect | event | change( event, ui ) | sortchange | Triggered during sorting, except when the DOM position changes.

    Code example:

    Initialize the sortable with the specified change callback:

    $( ".selector" ).sortable({
    
    change: function( event, ui ) {}
    
    });
    
        

    Bind an event listener to the sortchange event:

    $( ".selector" ).on( "sortchange", function( event, ui ) {} );
    
        
    | onSelect | event | create( event, ui ) | sortcreate | Triggered when the droppable is created.

    Note:ui The object is empty; it is included here for consistency with other events.

    Code example:

    Initialize the sortable with the specified create callback:

    $( ".selector" ).sortable({
    
    create: function( event, ui ) {}
    
    });
    
        

    Bind an event listener to the sortcreate event:

    $( ".selector" ).on( "sortcreate", function( event, ui ) {} );
    
        
    | onSelect | event | deactivate( event, ui ) | sortdeactivate | Triggered when sorting stops; the event propagates to all possible connected lists.

    Code example:

    Initialize the sortable with the specified deactivate callback:

    $( ".selector" ).sortable({
    
    deactivate: function( event, ui ) {}
    
    });
    
        

    Bind an event listener to the sortdeactivate event:

    $( ".selector" ).on( "sortdeactivate", function( event, ui ) {} );
    
        
    | onSelect | event | out( event, ui ) | sortout | Triggered when a sortable item is removed from a sortable list.

    Note: This event is also triggered when a sortable item is cancelled.

    Code example:

    Initialize the sortable with the specified out callback:

    $( ".selector" ).sortable({
    
    out: function( event, ui ) {}
    
    });
    
        

    Bind an event listener to the sortout event:

    $( ".selector" ).on( "sortout", function( event, ui ) {} );
    
        
    | onSelect | event | over( event, ui ) | sortover | Triggered when a sortable item is moved into a sortable list.

    Code example:

    Initialize the sortable with the specified over callback:

    $( ".selector" ).sortable({
    
    out: function( event, ui ) {}
    
    });
    
        

    Bind an event listener to the sortover event:

    $( ".selector" ).on( "sortout", function( event, ui ) {} );
    
        
    | onSelect | event | receive( event, ui ) | sortreceive | Triggered when an item from a connected sortable list is dropped into another list. The latter is the event target.

    Code example:

    Initialize the sortable with the specified receive callback:

    $( ".selector" ).sortable({
    
    receive: function( event, ui ) {}
    
    });
    
        

    Bind an event listener to the sortreceive event:

    $( ".selector" ).on( "sortreceive", function( event, ui ) {} );
    
        
    | onSelect | event | remove( event, ui ) | sortremove | Triggered when an item from a connected sortable list is dropped into another list. The former is the event target.

    Code example:

    Initialize the sortable with the specified remove callback:

    $( ".selector" ).sortable({
    
    remove: function( event, ui ) {}
    
    });
    
        

    Bind an event listener to the sortremove event:

    $( ".selector" ).on( "sortremove", function( event, ui ) {} );
    
        
    | onSelect | event | sort( event, ui ) | sort | Triggered during sorting.

    Code example:

    Initialize the sortable with the specified sort callback:

    $( ".selector" ).sortable({
    
    sort: function( event, ui ) {}
    
    });
    
        

    Bind an event listener to the sort event:

    $( ".selector" ).on( "sort", function( event, ui ) {} );
    
        
    | onSelect | event | start( event, ui ) | sortstart | Triggered when sorting starts.

    Code example:

    Initialize the sortable with the specified start callback:

    $( ".selector" ).sortable({
    
    start: function( event, ui ) {}
    
    });
    
        

    Bind an event listener to the sortstart event:

    $( ".selector" ).on( "sortstart", function( event, ui ) {} );
    
        
    | onSelect | event | stop( event, ui ) | sortstop | Triggered when sorting stops.

    Code example:

    Initialize the sortable with the specified stop callback:

    $( ".selector" ).sortable({
    
    stop: function( event, ui ) {}
    
    });
    
        

    Bind an event listener to the sortstop event:

    $( ".selector" ).on( "sortstop", function( event, ui ) {} );
    
        
    | onSelect | event | update( event, ui ) | sortupdate | Triggered when the user stops sorting and the DOM position has changed.

    Code example:

    Initialize the sortable with the specified update callback:

    $( ".selector" ).sortable({
    
    update: function( event, ui ) {}
    
    });
    
        

    Bind an event listener to the sortupdate event:

    $( ".selector" ).on( "sortupdate", function( event, ui ) {} );
    
        
    |

    Example

    A simple jQuery UI Sortable widget.

    
    
    
    
    
    
    
    
      
    
      可排序小部件(Sortable Widget)演示
    
      
    
      
    
      
    
    
    
    
    
     
    
    
    • Item 1
    • Item 2
    • Item 3
    • Item 4
    • Item 5