Categories

.sortable()

Categories: UI

.sortable( [ options ] )

Plugin: jQuery.ui.sortable

Description: Apply the Sortable widget for each element in the set of matched elements

  • .sortable( [ options ] )

    version added: 1.0

    options   A map of additional options pass to the widget.

The jQuery UI Sortable plugin makes selected elements sortable by dragging with the mouse.

All callbacks receive two arguments: The original browser event and a prepared ui object, view below for a documentation of this object (if you name your second argument 'ui'):

  • ui.helper - the current helper element (most often a clone of the item)
  • ui.position - current position of the helper
  • ui.offset - current absolute position of the helper
  • ui.item - the current dragged element
  • ui.placeholder - the placeholder (if you defined one)
  • ui.sender - the sortable where the item comes from (only exists if you move from one connected list to another)
  • disabled

    Disables (true) or enables (false) the sortable. Can be set when initialising (first creating) the sortable.

    Default: false

  • appendTo

    Defines where the helper that moves with the mouse is being appended to during the drag (for example, to resolve overlap/zIndex issues).

    Default: 'parent'

  • axis

    If defined, the items can be dragged only horizontally or vertically. Possible values:'x', 'y'.

    Default: false

  • cancel

    Prevents sorting if you start on elements matching the selector.

    Default: ':input,button'

  • connectWith

    Takes a jQuery selector with items that also have sortables applied. If used, the sortable is now connected to the other one-way, so you can drag from this sortable to the other.

    Default: false

  • containment

    Constrains dragging to within the bounds of the specified element - can be a DOM element, 'parent', 'document', 'window', or a jQuery selector. Note: the element specified for containment must have a calculated width and height (though it need not be explicit), so for example, if you have float:left sortable children and specify containment:'parent' be sure to have float:left on the sortable/parent container as well or it will have height: 0, causing undefined behavior.

    Default: false

  • cursor

    Defines the cursor that is being shown while sorting.

    Default: 'auto'

  • cursorAt

    Moves the sorting element or helper so the cursor always appears to drag from the same position. Coordinates can be given as a hash using a combination of one or two keys: { top, left, right, bottom }.

    Default: false

  • delay

    Time in milliseconds to define when the sorting should start. It helps preventing unwanted drags when clicking on an element.

  • distance

    Tolerance, in pixels, for when sorting should start. If specified, sorting will not start until after mouse is dragged beyond distance. Can be used to allow for clicks on elements within a handle.

    Default: 1

  • dropOnEmpty

    If false items from this sortable can't be dropped to an empty linked sortable.

    Default: true

  • forceHelperSize

    If true, forces the helper to have a size.

    Default: false

  • forcePlaceholderSize

    If true, forces the placeholder to have a size.

    Default: false

  • grid

    Snaps the sorting element or helper to a grid, every x and y pixels. Array values: [x, y]

    Default: false

  • handle

    Restricts sort start click to the specified element.

    Default: false

  • helper

    Allows for a helper element to be used for dragging display. The supplied function receives the event and the element being sorted, and should return a DOMElement to be used as a custom proxy helper. Possible values: 'original', 'clone'

    Default: 'original'

  • items

    Specifies which items inside the element should be sortable.

    Default: '> *'

  • opacity

    Defines the opacity of the helper while sorting. From 0.01 to 1

    Default: false

  • placeholder

    Class that gets applied to the otherwise white space.

    Default: false

  • revert

    If set to true, the item will be reverted to its new DOM position with a smooth animation. Optionally, it can also be set to a number that controls the duration of the animation in ms.

    Default: false

  • scroll

    If set to true, the page scrolls when coming to an edge.

    Default: true

  • scrollSensitivity

    Defines how near the mouse must be to an edge to start scrolling.

    Default: 20

  • scrollSpeed

    The speed at which the window should scroll once the mouse pointer gets within the scrollSensitivity distance.

    Default: 20

  • tolerance

    This is the way the reordering behaves during drag. Possible values: 'intersect', 'pointer'. In some setups, 'pointer' is more natural.

    Default: 'intersect'

  • zIndex

    Z-index for element/helper while being sorted.

    Default: 1000

  • create

    This event is triggered when sortable is created.

  • start

    This event is triggered when sorting starts.

  • sort

    This event is triggered during sorting.

  • change

    This event is triggered during sorting, but only when the DOM position has changed.

  • beforeStop

    This event is triggered when sorting stops, but when the placeholder/helper is still available.

  • stop

    This event is triggered when sorting has stopped.

  • update

    This event is triggered when the user stopped sorting and the DOM position has changed.

  • receive

    This event is triggered when a connected sortable list has received an item from another list.

  • remove

    This event is triggered when a sortable item has been dragged out from the list and into another.

  • over

    This event is triggered when a sortable item is moved into a connected list.

  • out

    This event is triggered when a sortable item is moved away from a connected list.

  • activate

    This event is triggered when using connected lists, every connected list on drag start receives it.

  • deactivate

    This event is triggered when sorting was stopped, is propagated to all possible connected lists.

  • destroy

    • .sortable( "destroy" )

      version added: 1.0

    Remove the sortable functionality completely. This will return the element back to its pre-init state.

  • disable

    • .sortable( "disable" )

      version added: 1.0

    Disable the sortable.

  • enable

    • .sortable( "enable" )

      version added: 1.0

    Enable the sortable.

  • option

    • .sortable( "option" , optionName , [value] )

      version added: 1.0

    Get or set any sortable option. If no value is specified, will act as a getter.

  • option

    • .sortable( "option" , options )

      version added: 1.0

    Set multiple sortable options at once by providing an options object.

  • widget

    • .sortable( "widget" )

      version added: 1.0

    Returns the .ui-sortable element.

  • serialize

    • .sortable( "serialize" , [options] )

      version added: 1.0

    Serializes the sortable's item id's into a form/ajax submittable string. Calling this method produces a hash that can be appended to any url to easily submit a new item order back to the server.

    It works by default by looking at the id of each item in the format 'setname_number', and it spits out a hash like "setname[]=number&setname[]=number".

    You can also give in a option hash as second argument to custom define how the function works. The possible options are: 'key' (replaces part1[] with whatever you want), 'attribute' (test another attribute than 'id') and 'expression' (use your own regexp).

    If serialize returns an empty string, make sure the id attributes include an underscore. They must be in the form: "set_number" For example, a 3 element list with id attributes foo_1, foo_5, foo_2 will serialize to foo[]=1&foo[]=5&foo[]=2. You can use an underscore, equal sign or hyphen to separate the set and number. For example foo=1 or foo-1 or foo_1 all serialize to foo[]=1.

  • toArray

    • .sortable( "toArray" )

      version added: 1.0

    Serializes the sortable's item id's into an array of string. If you have

    <ul id="a_sortable"><br>
    <li id="hello">Hello</li><br>
    <li id="goodbye">Good bye</li><br>
    </ul>
    

    and do

    var result = $('#a_sortable').sortable('toArray');

    then

    result[0] will contain "hello" and result[1] will contain "goodbye".
  • refresh

    • .sortable( "refresh" )

      version added: 1.0

    Refresh the sortable items. Custom trigger the reloading of all sortable items, causing new items to be recognized.

  • refreshPositions

    • .sortable( "refreshPositions" )

      version added: 1.0

    Refresh the cached positions of the sortables' items. Calling this method refreshes the cached item positions of all sortables. This is usually done automatically by the script and slows down performance. Use wisely.

  • cancel

    • .sortable( "cancel" )

      version added: 1.0

    Cancels a change in the current sortable and reverts it back to how it was before the current sort started. Useful in the stop and receive callback functions.

    If the sortable item is not being moved from one connected sortable to another:

    $(this).sortable('cancel');

    will cancel the change.

    If the sortable item is being moved from one connected sortable to another:

    $(ui.sender).sortable('cancel');

    will cancel the change. Useful in the 'receive' callback.