jQuery Sortable Tutorial 🎯

beginner
14 min

jQuery Sortable Tutorial 🎯

Welcome to our deep dive into the Sortable feature of jQuery! This tutorial is designed to help both beginners and intermediates understand and master this powerful tool. Let's get started!

Understanding Sortable 📝

Sortable is a jQuery plugin that allows you to create interactive, drag-and-drop lists on your web pages. It's incredibly useful for organizing data, managing UI components, and improving user experience.

Why Use Sortable? 💡

  • Enhances user interaction and engagement
  • Simplifies list management in dynamic web applications
  • Supports multiple layout modes and list types

Getting Started 🚀

To use Sortable, you'll first need to include jQuery in your project. If you haven't done so already, you can find the latest version on the official jQuery website.

Next, download the Sortable plugin from the jQuery UI website. Include it along with the core jQuery library in your HTML file.

html
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>

Creating a Sortable List 📝

Now, let's create a basic Sortable list. We'll use the sortable() method to activate the Sortable functionality on an HTML list (<ul>).

html
<ul id="sortable-list"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <li>Item 4</li> </ul>
javascript
$(function() { $("#sortable-list").sortable(); });

In the JavaScript code above, we initialize the Sortable feature on the list with the ID sortable-list.

List Types 📝

jQuery Sortable supports three list types:

  1. Held: The default list type, where items are selected by clicking and dragging.
  2. ConnectedSortable: For creating a connection between two lists.
  3. NestedSortable: For nesting lists within other lists.

Advanced Examples 💡

Let's explore a more practical example involving multiple lists and nested lists.

HTML:

html
<div id="container"> <ul id="list1"> <li>Item 1</li> <li>Item 2</li> </ul> <ul id="list2"> <li>Item 3</li> <li>Item 4</li> </ul> <ul id="nested-list"> <li>Nested Item 1</li> <li>Nested Item 2</li> <ul id="sub-nested-list"> <li>Sub-nested Item 1</li> <li>Sub-nested Item 2</li> </ul> </ul> </div>

JavaScript:

javascript
$(function() { $("#container").sortable({ connectWith: ".connectedSortable" }); $("#list1, #list2").sortable({ connectWith: "#container" }); $("#nested-list").sortable({ connectWith: "#container", nestable: true, handle: ".handle" }); });

In this example, we have three lists (list1, list2, and nested-list). We connect them with the container #container, enabling drag-and-drop between them. We also create a nested list with the sub-nested list and set the handle class to be used for dragging.

Quiz Time 🎯

Quick Quiz
Question 1 of 1

What are the three list types supported by jQuery Sortable?

That's it for our Sortable tutorial! Remember to practice, experiment, and create your own interactive Sortable lists in your projects. Happy coding! 🎉