Sortable
Reorder elements in a list or across multiple lists.
Usage
The Sortable
class allows you to reorder elements in a list or across multiple lists. A sortable element is both Droppable and Draggable, which means you can drag it and drop it to reorder.
First, create a DragDropManager instance and use it to create sortable items:
Multiple Lists
You can create multiple sortable lists by assigning sortable items to different groups:
Drag Handles
By default, the entire sortable element can be used to initiate dragging. You can restrict dragging to a specific handle element:
Animations
Sortable items automatically animate when their position changes. You can customize the animation through the transition
option:
API Reference
Arguments
The Sortable
class accepts the following arguments:
A unique identifier for this sortable item within the drag and drop manager.
The position of this item within its sortable group.
The DOM element to make sortable. While not required in the constructor, it must be set to enable sorting.
Optionally assign this item to a group. Items can only be sorted within their group.
Optionally specify a drag handle element. If not provided, the entire element will be draggable.
Optionally specify a different element to use as the drop target. By default, uses the main element.
Configure the animation when items are reordered:
Set to true
to temporarily disable sorting for this item.
Optionally restrict which types of items can be sorted together.
Optionally restrict which types of items can be dropped on this item.
Optional data to associate with this sortable item, available in event handlers.
Properties
The Sortable
instance provides these key properties:
index
: The current position in the listgroup
: The assigned group identifierisDragging
: Whether this item is currently being draggedisDropTarget
: Whether this item is currently a drop targetdisabled
: Whether sorting is disabled for this itemelement
: The main DOM elementtarget
: The drop target element (if different from main element)
Methods
register()
: Register this sortable item with the managerunregister()
: Remove this item from the managerdestroy()
: Clean up this sortable instanceaccepts(draggable)
: Check if this item accepts a draggablerefreshShape()
: Recalculate the item’s dimensions