Jqueryui Sortable Features and Compatibility Options

Author

Reads 276

Creative flat lay featuring wooden pieces, geometric shapes, and natural elements on a table.
Credit: pexels.com, Creative flat lay featuring wooden pieces, geometric shapes, and natural elements on a table.

Jqueryui Sortable offers a wide range of features to enhance your sorting experience, including multiple widget sorting and sorting with multiple handles.

You can also use the helper option to customize the appearance of the helper element, which is a clone of the item being dragged.

The opacity option allows you to set the opacity of the helper element, which can be useful for creating a more subtle sorting effect.

Jqueryui Sortable is compatible with a variety of browsers, including Internet Explorer, Firefox, Safari, and Chrome.

It also supports touch events, making it a great option for mobile devices.

On a similar theme: Jqueryui Animate

Events

The jQuery UI Sortable plugin makes selected elements sortable by dragging with the mouse. To make table rows sortable, the tbody must be made sortable, not the table.

You can initialize the sortable with various callbacks, such as create, receive, sort, out, over, update, activate, start, and stop. These callbacks provide information about the event and the UI object.

You might like: Jquery Ui Autocomplete

Credit: youtube.com, 004 jQuery UI Sortable

Here's a list of some of the events you can bind to:

  • sortcreate: triggered when the sortable is created
  • sortreceive: triggered when a sortable item is received
  • sort: triggered when the user starts sorting
  • sortout: triggered when a sortable item is moved away from a sortable list
  • sortover: triggered when a sortable item is dragged over another item
  • sortupdate: triggered when the user finishes sorting
  • sortablebeforestop: triggered before the sortable stops
  • sortablechange: triggered when the sortable changes
  • sortablestart: triggered when the sortable starts
  • sortablestop: triggered when the sortable stops
  • sortableupdate: triggered when the sortable updates

Update Event

The update event is a crucial part of the jQuery UI Sortable plugin, allowing you to execute a callback function when the sort operation is complete.

To initialize the sortable with the update callback specified, you can use the following code: $( ".selector" ).sortable({update: function( event, ui ) {}});

You can also bind an event listener to the sortupdate event using the following code: $( ".selector" ).on( "sortupdate", function( event, ui ) {} );

The sortupdate event is triggered after the sort operation has completed, and it passes the event and ui objects as arguments to the callback function.

Here's a summary of the update event:

Note that the sortupdate and sortableupdate events are essentially the same, with the latter being a more specific event type for the sortable plugin.

Out Event

The out event is triggered when a sortable item is moved away from a sortable list. This event is also triggered when a sortable item is dropped.

From above crop anonymous female in white blouse sitting at wooden table with pen and making list of plans for month
Credit: pexels.com, From above crop anonymous female in white blouse sitting at wooden table with pen and making list of plans for month

The out event is a type of sortout event. It can be initialized with a callback function that takes two parameters: event and ui.

To bind an event listener to the sortout event, you can use the on method. The syntax is $( ".selector" ).on( "sortout", function( event, ui ) {} );.

Here's a simple way to initialize the sortable with the out callback specified: $( ".selector" ).sortable({out: function( event, ui ) {}});.

A fresh viewpoint: Jquery Ui Accordion

Theming

The jQuery UI CSS framework is used to style the look and feel of the sortable widget.

The framework provides a set of CSS class names that can be used for overrides or as keys for the classes option.

The sortable element can be targeted using the CSS class name "ui-sortable".

The element shown while dragging a sortable item can be targeted using the CSS class name "ui-sortable-helper".

Broaden your view: Ui Jquery Dialog

Grid

You can customize the grid layout of a sortable element by using the grid option. This option allows you to specify the number of rows and columns.

View of city skyline framed by a modern metal grid structure, showing architectural innovation.
Credit: pexels.com, View of city skyline framed by a modern metal grid structure, showing architectural innovation.

To initialize a sortable with a grid, you can use the following code: $( ".selector" ).sortable({grid: [ 20, 10 ]});

You can also get or set the grid option after initialization. To do this, you can use the "option" method: var grid = $( ".selector" ).sortable( "option", "grid" ); or $( ".selector" ).sortable( "option", "grid", [ 20, 10 ] );

This means you can easily switch between different grid layouts or retrieve the current grid settings.

Opacity

Opacity is a crucial aspect of theming, allowing you to control the transparency of elements on your page.

You can initialize a sortable with the opacity option specified, like this: $( ".selector" ).sortable({opacity: 0.5}).

This sets the opacity to 0.5, making the elements slightly transparent.

You can also get or set the opacity option after initialization using the sortable method. For example, var opacity = $( ".selector" ).sortable( "option", "opacity" ); sets the opacity to the value specified.

Configuration

Credit: youtube.com, Refactoring and Setting up jQuery Sortable Part 17

To configure jQuery UI Sortable, you need to initialize it on the element you want to make sortable. This is done by calling the sortable method on the element.

The default behavior of jQuery UI Sortable is to sort items in the same list, but you can change this by setting the connectWith option to a selector that matches another list. This allows you to create a multi-list sortable where items can be moved between lists.

The containment option can also be set to a selector that matches a parent element, restricting the sortable items to be dragged within that element.

Append To

Append To is an option that allows you to specify where the helper element should be appended to when sorting items. This option can be initialized during the sorting process.

You can initialize the sortable with the appendTo option specified by passing a jQuery object, an element, a selector, or the string "parent" to the appendTo option. For example, $( ".selector" ).sortable({appendTo: document.body}).

The appendTo option can also be set after initialization using the "option" method. For instance, you can use $( ".selector" ).sortable( "option", "appendTo", document.body ).

This method does not accept any arguments, so you can simply call it with the new value.

Cancel Plugin Only

Bright pink to-do list notes arranged on a green leafy patterned background, creating an organized and vibrant workspace vibe.
Credit: pexels.com, Bright pink to-do list notes arranged on a green leafy patterned background, creating an organized and vibrant workspace vibe.

The cancel plugin only is a feature of the sortable plugin, and it's used to prevent certain elements from being sortable. You can specify which elements to cancel sorting on by passing a string to the cancel option.

To invoke the cancel method, you can use the cancel() method, which returns the jQuery object. This is useful for chaining methods together. For example, you can use it to destroy the sortable after canceling it.

Here's a simple example of how to use the cancel method: $( ".selector" ).sortable( "cancel" );. This will cancel the sortable and return the jQuery object.

Configuration

Configuration is where the magic happens. You can customize the sortable plugin to suit your needs by using the `option()` method.

To set the value of a sortable option, you can use the `option(optionName, value)` method. For example, to update the `foo.bar` property of the `foo` option, you would use the following code: `$( ".selector" ).sortable( "option", "foo.bar", value )`.

To Do List On Orange Surface
Credit: pexels.com, To Do List On Orange Surface

The `optionName` parameter is a string that specifies the name of the option to set. You can also use dot notation to set the value of a specific property within an object. The `value` parameter is the new value to set for the option.

You can use the `instance()` method to retrieve the sortable's instance object. This method is safe to call on any element after the sortable plugin has loaded.

Here's a list of the `option()` method's parameters:

  • optionName: String - The name of the option to set.
  • value: Object - A value to set for the option.

The `instance()` method returns the sortable's instance object, or `undefined` if the element does not have an associated instance.

Containment

Containment is a crucial aspect of configuring sortable elements, and it's essential to understand how it works.

The containment option defines a bounding box that the sortable items are constrained to while dragging. This means that the items will stay within the specified area.

To specify the containment, you can use an element, a selector, or a string. The possible values for the string are "parent", "document", or "window".

Credit: youtube.com, Setting up the Containment Wall

For example, if you want to constrain the items to a specific parent element, you would use the string "parent". You can also use a selector to specify the container element.

If you have float: left sortable children and specify containment: "parent", make sure to have float: left on the sortable/parent container as well, or it will have height: 0, causing undefined behavior.

You can initialize the sortable with the containment option specified like this: $( ".selector" ).sortable({containment: "parent"});.

You can also get or set the containment option after initialization using the "option" method. For example, to get the containment option, you would use: var containment = $( ".selector" ).sortable( "option", "containment" );.

Here are the possible values for the containment option:

Delay

The delay option is a useful feature in sortable elements, allowing you to specify a delay in milliseconds before sorting starts.

You can initialize the sortable with the delay option specified, like this: $( ".selector" ).sortable({delay: 150}).

To get or set the delay option after initialization, use the "option" method, like this: var delay = $( ".selector" ).sortable( "option", "delay" ); or $( ".selector" ).sortable( "option", "delay", 150 ).

This delay can be set to a specific number of milliseconds, such as 150 milliseconds.

Force Helper Size

Open notebook with a list of goals under 'Less' and 'More', pen beside it, on a brown background.
Credit: pexels.com, Open notebook with a list of goals under 'Less' and 'More', pen beside it, on a brown background.

Force Helper Size is a configuration option that helps you fine-tune the behavior of your sortable elements.

You can initialize the sortable with the forceHelperSize option specified by passing true as an argument: $( ".selector" ).sortable({forceHelperSize: true}).

This option determines whether the helper element should be resized to match the size of the item being moved.

Force Placeholder Size

Force Placeholder Size is a configuration option that helps control the size of the placeholder when dragging items to rearrange them.

You can specify the forcePlaceholderSize option when initializing a sortable, and it will be applied immediately.

To get or set the forcePlaceholderSize option after initialization, use the "option" method of the sortable instance. For example, you can use the following code to get the current value of forcePlaceholderSize: var forcePlaceholderSize = $(".selector").sortable("option", "forcePlaceholderSize");

You can also use the "option" method to set a new value for forcePlaceholderSize. For example, to set forcePlaceholderSize to true, use the following code: $(".selector").sortable("option", "forcePlaceholderSize", true);

Here's a summary of the forcePlaceholderSize option:

Z-Index

Credit: youtube.com, Solve your z-index issues | z-index and stacking context explained

You can initialize the sortable with a specific z-index value using the zIndex option. This sets the stacking order of elements within the sortable container.

To specify the z-index value, simply add it to the sortable initialization like this: $( ".selector" ).sortable({zIndex: 9999});

If you want to get or set the z-index value after the sortable has been initialized, you can use the "option" method. To get the current z-index, use the following code: var zIndex = $( ".selector" ).sortable( "option", "zIndex" );

To change the z-index value, use the following code: $( ".selector" ).sortable( "option", "zIndex", 9999 );

For your interest: No Code Html Editor

Behavior

You can achieve a lot with the jQuery UI Sortable functionality, and one of the key aspects is its behavior. This behavior can be demonstrated with a simple example, where passing no parameters to the sortable() method still gets the job done.

The default functionality is pretty straightforward, and it's easily seen in action when you save the code in an HTML file and open it in a standard browser that supports JavaScript.

Cancel

Pen on to Do List Paper
Credit: pexels.com, Pen on to Do List Paper

The cancel option in jQuery UI Sortable is a powerful tool for controlling user interaction.

You can specify elements to cancel sorting for by passing a string to the cancel option, like "a,button". For instance, $( ".selector" ).sortable({cancel: "a,button"}); will cancel sorting when the user tries to drag an anchor tag or a button.

To get or set the cancel option, you can use the option method. For example, var cancel = $( ".selector" ).sortable( "option", "cancel" ); will get the current cancel option, while $( ".selector" ).sortable( "option", "cancel", "a,button" ); will set the cancel option to "a,button".

The cancel method, available in the plugin only, is used to cancel the current sorting operation. It returns the jQuery object, so you can chain other methods to it. For instance, $( ".selector" ).sortable( "cancel" ); will cancel the current sorting operation.

You can also use the destroy method to cancel the sorting operation and destroy the sortable.

What Does It Do?

A List on a Wall
Credit: pexels.com, A List on a Wall

Sortable functionality allows you to sort any items in any container. It fully supports nested containers, making it a versatile tool.

One of the key features of sortable functionality is its ability to connect lists. This means you can drag items from one list to another, even if they're not adjacent to each other. According to Example 4, you can pass a selector into the connectWith option to achieve this.

Sortable functionality also supports callbacks and events, as mentioned in Example 6. This allows you to customize the behavior of your sortable lists to suit your needs.

You can make a list sortable in just 3 easy steps, as listed in Example 6:

  • Sorts any items in any container
  • Fully supports nested containers
  • Connect lists
  • Callbacks and events (see docs)
  • Pure drag/drop lists
  • Vertical and horizontal sorting

By following these steps, you can create sortable lists that are both functional and user-friendly.

Cursor

The cursor option is a crucial aspect of making your sortable elements user-friendly. You can initialize the sortable with the cursor option specified, like this: $( ".selector" ).sortable({cursor: "move"});.

Credit: youtube.com, Cursor Jumping or Unexpected cursor behaviour [SOLVED}

To customize the cursor, you can get or set the cursor option after initialization. For instance, you can use the following code to get the current cursor: var cursor = $( ".selector" ).sortable( "option", "cursor" );, or set it to a new value: $( ".selector" ).sortable( "option", "cursor", "move" );.

The cursor option can be set to any valid CSS cursor value, such as "move", "crosshair", or "text".

Drop on Empty

Drop on Empty is an option in jQuery UI Sortable that determines whether an item can be dropped onto an empty list. It's set to true by default, allowing items to be dropped into empty lists.

To get or set the dropOnEmpty option after initialization, you can use the following code: `$( ".selector" ).sortable( "option", "dropOnEmpty" );` This code will return the current value of the dropOnEmpty option, and the second part of the code sets the dropOnEmpty option to false.

Woman Reading a List of Grocery
Credit: pexels.com, Woman Reading a List of Grocery

You can also set the dropOnEmpty option when initializing the sortable with the following code: `$( ".selector" ).sortable({dropOnEmpty: false});` This will prevent items from being dropped into empty lists.

Here are some examples of how to use the dropOnEmpty option:

If you want to allow items to be dropped into empty lists, you can set the dropOnEmpty option to true. If you want to prevent items from being dropped into empty lists, you can set the dropOnEmpty option to false.

Handle

The handle option is a great way to customize the behavior of your sortable elements. You can specify a handle element that users can drag to sort items.

To initialize the sortable with a handle, you can use the handle option like this: $( ".selector" ).sortable({handle: ".handle"});. This tells the sortable to use the element with the class "handle" as the drag handle.

You can get or set the handle option after initialization using the "option" method: var handle = $( ".selector" ).sortable( "option", "handle" );. This is useful if you want to change the handle element later.

For your interest: Html Drag Drop File Upload

A Close-Up Shot of a To-Do List
Credit: pexels.com, A Close-Up Shot of a To-Do List

The handle option can be a specific element, like a span or a div, or it can be a class that is applied to multiple elements. This allows you to have multiple handles in a single sortable element.

Here are some common use cases for the handle option:

By using the handle option, you can customize the behavior of your sortable elements to fit your specific needs.

Helper

The helper option is a crucial part of the sortable behavior. It determines how the element will be dragged during sorting.

You can set the helper option to "clone" to clone the element and drag the clone instead. This is useful for creating a visual representation of the moved element without actually moving it.

A function can also be used as the helper option. This function must return a jQuery or DOMElement to use while dragging. It receives the event and the jQuery element being sorted.

Photograph of a Man Looking at a List
Credit: pexels.com, Photograph of a Man Looking at a List

Here are the possible values for the helper option:

  • String: "clone" to clone the element and drag the clone.
  • Function: a function that returns a jQuery or DOMElement to use while dragging.

By setting the helper option, you can customize the behavior of the sortable and create a more user-friendly experience.

Items

The items option in jQuery UI Sortable is a powerful tool for customizing the behavior of your sortable elements. You can specify the items option when initializing the sortable, like so: $( ".selector" ).sortable({items: "> li"}).

To get or set the items option after initialization, you can use the "option" method. For example, to get the current items option, you can use the following code: var items = $( ".selector" ).sortable( "option", "items" ). The result will be the current items option.

The items option can be set to a selector, such as "> li", which means only list items will be sortable. You can also set it to a function, but that's a more advanced topic.

Here are the possible values for the items option:

  • "li" - only list items will be sortable
  • "li > li" - only direct children of list items will be sortable

By specifying the items option, you can control which elements can be sorted and which cannot. This can be especially useful when you have a complex layout with multiple types of elements.

Placeholder

Over-the-shoulder view of a person using a laptop indoors with a placeholder screen for customization.
Credit: pexels.com, Over-the-shoulder view of a person using a laptop indoors with a placeholder screen for customization.

The placeholder feature in sortable is a game-changer. It allows you to style the space where an item will be moved to, giving users a clear visual cue of where their item will end up.

By initializing the sortable with the placeholder option specified, you can customize the look and feel of this space. For example, you can use the code $( ".selector" ).sortable({placeholder: "sortable-placeholder"}); to add a class to the placeholder element.

The placeholder option can also be used to animate the items when they're being moved, which can enhance the user experience. You can set it to true to animate with the default duration, or specify a custom duration in milliseconds.

Here are some key things to know about the placeholder option:

  • Boolean: When set to true, the items will animate with the default duration.
  • Number: The duration for the animation, in milliseconds.

By using the placeholder feature, you can create a more intuitive and user-friendly interface for rearranging items.

Revert

The Revert option is a powerful tool in making your sortable lists more user-friendly. You can initialize it by specifying the revert option in the sortable function.

A child's hands writing a Christmas list on paper, emphasizing the joy of holiday preparations.
Credit: pexels.com, A child's hands writing a Christmas list on paper, emphasizing the joy of holiday preparations.

To get or set the revert option, you can use the "option" method. For example, you can get the current state of the revert option with $( ".selector" ).sortable( "option", "revert" );. This returns the current value of the revert option.

You can also set the revert option to true with $( ".selector" ).sortable( "option", "revert", true );. This sets the revert option to true, allowing the list to revert to its original position after a drag.

By using the revert option, you can make your sortable lists more intuitive and user-friendly.

Return

Return is a crucial aspect of behavior, especially when it comes to the sortable plugin. You can return to the default behavior of the plugin by setting the scroll option to false.

The scroll option is used to specify whether the plugin should scroll to the dropped item. By setting it to false, you can prevent the plugin from scrolling to the dropped item.

Close-Up Shot of App Icons
Credit: pexels.com, Close-Up Shot of App Icons

You can also use the stop callback to return to the default behavior of the plugin. The stop callback is triggered when the sorting is stopped. By using the stop callback, you can perform any necessary actions before returning to the default behavior.

Here are some examples of how to use the stop callback to return to the default behavior:

In the example below, the stop callback is used to prevent the plugin from scrolling to the dropped item:

$( ".selector" ).on( "sortstop", function( event, ui ) { $( ".selector" ).sortable( "option", "scroll", false ); } );

Sensitivity

Sensitivity is an important aspect of behavior, and it's something that can be controlled with the scrollSensitivity option.

You can initialize the sortable with a specified scrollSensitivity, like $( ".selector" ).sortable({scrollSensitivity: 10}).

The scrollSensitivity option can be used to adjust how sensitive the sortable is to scrolling. For example, a higher value like 10 can make the sortable more sensitive to scrolling.

Photo of Periodic Table of Elements
Credit: pexels.com, Photo of Periodic Table of Elements

To get or set the scrollSensitivity option after initialization, you can use the "option" method, like this: $( ".selector" ).sortable( "option", "scrollSensitivity" ); or $( ".selector" ).sortable( "option", "scrollSensitivity", 10 );.

Here are some examples of scrollSensitivity values and what they might do:

By adjusting the scrollSensitivity option, you can fine-tune the behavior of your sortable to fit your needs.

Speed

Speed is a crucial aspect of making your sortable elements feel smooth and responsive. You can control the speed of the sortable elements with the scrollSpeed option.

The scrollSpeed option determines how fast the sortable elements scroll when you drag them. You can initialize the sortable with the scrollSpeed option specified, like this: $( ".selector" ).sortable({scrollSpeed: 40}).

The default value for scrollSpeed is not specified in the examples, so you'll need to set it manually to achieve the desired speed. You can also get or set the scrollSpeed option after initialization using the "option" method.

Credit: youtube.com, Basics in behavior – speed up

Here's a quick reference to the scrollSpeed option:

For example, to get the current scrollSpeed, you can use the "option" method like this: var scrollSpeed = $( ".selector" ).sortable( "option", "scrollSpeed" ).

Z Index

The z-index option allows you to control the stacking order of elements within a sortable.

You can initialize a sortable with a specified z-index, for example, by setting it to 9999.

To get or set the z-index option after initialization, use the "option" method.

For instance, you can get the current z-index with $( ".selector" ).sortable( "option", "z-index" ).

Or, you can set a new z-index value with $( ".selector" ).sortable( "option", "z-index", 9999 ).

Activate

The activate event is triggered when the user starts to sort an item in a sortable list. It's a crucial part of the sortable functionality.

You can initialize the sortable with the activate callback specified, like this: `$( ".selector" ).sortable({activate: function( event, ui ) {}});`. This sets up the event listener for the activate event.

Close up Shot of a Person Looking at Photos on a Smartphone
Credit: pexels.com, Close up Shot of a Person Looking at Photos on a Smartphone

The activate event passes two parameters: `eventType` and `ui`. `eventType` is an `Event` object, and `ui` is an `Object`.

Here are the details of the parameters:

You can also bind an event listener to the `sortactivate` event using the `on` method, like this: `$( ".selector" ).on( "sortactivate", function( event, ui ) {} );`. This is a shorthand way to set up the event listener.

Note that the event name is `sortactivate`, not `sortableactivate`.

Disable

Disabling behavior is a straightforward process. You can use the method ".sortable("disable")" to instantly stop all sortables in a set of matched elements from functioning.

This method is particularly useful when you need to temporarily pause sorting functionality, perhaps to prevent users from interacting with a particular section of your website.

By calling ".sortable("disable")", you can effectively halt the sorting behavior, giving you more control over how your users interact with your content.

This approach can be especially helpful when you're dealing with dynamic content or complex user interfaces where sorting behavior needs to be toggled on and off.

Refresh

Credit: youtube.com, refresh behavior

Refreshing your behavior can be a game-changer. A study found that small changes in daily habits can lead to significant improvements in well-being.

Getting enough sleep is crucial for refreshing your behavior. Research suggests that even a single night of poor sleep can impair cognitive function and judgment.

Taking breaks throughout the day is essential for maintaining focus and productivity. A study showed that employees who took regular breaks performed better and were more efficient than those who didn't.

Engaging in physical activity can also help refresh your behavior. Exercise has been shown to improve mood and reduce stress levels.

A change of environment can be just what you need to refresh your behavior. A study found that workers who took breaks outside performed better and were more productive than those who stayed indoors.

Destroy

Destroying a plugin can be a useful technique in certain situations. The sortable plugin can be removed from a set of matched elements using the method ".sortable("destroy")".

This method is listed in alphabetical order, along with other methods that can be used to manage plugins. The destroy method is a straightforward way to remove a plugin and restore the original behavior of the elements.

Example

In the context of Behavior, specifically with jQuery UI Sortable, it's essential to understand the example implementations.

Person Writing To Do List with Ink Pen
Credit: pexels.com, Person Writing To Do List with Ink Pen

A simple jQuery UI Sortable is achieved by adding a script that enables sorting functionality to an unordered list.

The required jQuery and jQuery UI libraries need to be included in the HTML document for the sortable functionality to work.

By referencing the jQuery library, you can apply the sortable functionality to a specific element, such as an unordered list with the id "sortable".

For instance, in Example 1, the jQuery library version 1.10.2 is used, while in Example 2, jQuery library version 1.8.2 is referenced.

The jQuery UI library is also necessary to enable the sortable functionality, and its version can be seen in the HTML document, such as in Example 1, where version 1.11.4 is used, and in Example 2, version 1.8.24 is referenced.

To make an element sortable, you need to apply the sortable method to it, which can be done by using a script like the one in Example 1 and Example 2, where the script $("#sortable").sortable(); is used.

Thomas Goodwin

Lead Writer

Thomas Goodwin is a seasoned writer with a passion for exploring the intersection of technology and business. With a keen eye for detail and a knack for simplifying complex concepts, he has established himself as a trusted voice in the tech industry. Thomas's writing portfolio spans a range of topics, including Azure Virtual Desktop and Cloud Computing Costs.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.