Shaun Mccran

My digital playground

17
J
U
N
2011

JQuery Sortable Drag and Drop lists and a server side AJAX save

I was recently asked this question about creating and using elements with the JQuery drag and drop functions:

view plain print about
1Shaun, I'd like to be able to drag items (let's say grocery items) onto a list (let's say a grocery list) using JQuery and PHP to process and save the list. I found you drag and drop tutorial but I want the droppable to be in a list form. Is this easy?

This article explains how you can use a few JQuery interactions to create a sortable drag and drop list and save its state to a server side processing template.

Firstly you must include a series of JQuery script files. These are the core files and a collection of interaction manager files, things like mouse.js, sortable.js, draggable.js and droppable.js.

view plain print about
1<s/cript type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>
2
3<s/cript src="jquery.ui.core.js"></script>
4<s/cript src="jquery.ui.widget.js"></script>
5
6<s/cript src="jquery.ui.mouse.js"></script>
7<s/cript src="jquery.ui.draggable.js"></script>
8<s/cript src="jquery.ui.droppable.js"></script>
9<s/cript src="jquery.ui.sortable.js"></script>

Next create two div elements. The first of which holds our list of items. This is a html list that holds each item. I've also given each item in the list a unique id as this will be used as our item reference later on, to save back to the server.

The second div element is a sortable list. This list will hold our dragged items.

view plain print about
1<div class="shop-container">
2    <ul>
3    <li class="draggable" id="bread">
4Bread <img src="bread.png" class="img-float">
5</li>
6    <li class="draggable" id="milk">
7Milk <img src="milk.jpg" class="img-float">
8</li>
9    <li class="draggable" id="cheese">
10Cheese <img src="cheese.gif" class="img-float">
11</li>
12    <li class="draggable" id="oranges">
13Oranges <img src="orange.gif" class="img-float">
14</li>
15    </ul>
16
17</div>
18
19<ul id="sortable" class="sortable"></ul>

This will give us a list and an empty area with no interaction at all. To allow the list items to be dragged and the sortable area to accept and hold them we need to write a function that uses their selectors as identifiers for the JQuery interactions.

The code below has a selector of 'draggable' to enable the list items to be dragged around. It also uses the 'connectToSortable' value to pair it with the 'sortable' div element. Only by doing this do we allow the two div's to interact.

The first function uses the 'sortable' class to enabling sorting and dropping on the corresponding div. It also has an 'update' event that fires whenever its internal list is updated. This creates an Array of the list items and fires them through an AJAX request to a server side template where you can process them however you want.

For demo purposes I am also alerting() the list item values.

view plain print about
1<s/cript>
2    $(function() {
3        $( "#sortable" ).sortable({ revert: true,
4
5            update: function( event, ui ) {
6
7                var basketItems = $(this).sortable('toArray').toString();
8                alert(basketItems);
9                $.get('updatebasket.cfm', {basketItems:basketItems});
10
11            }
12
13        });
14
15        $( ".draggable" ).draggable({
16            connectToSortable: "#sortable",
17            helper: "clone",
18            revert: "invalid"
19        });
20
21        $( "ul, li" ).disableSelection();
22    });
23    </script>

It seems like a pretty flexible way of creating a shopping basket or sorting system but I've yet to find anywhere to use it in a production environment.

There is a demo of this working here: http://www.mccran.co.uk/examples/jquery-multi-drag-drop/

TweetBacks
Comments
mgfranz's Gravatar Great use of Drag & Drop, one question though. What if you want to remove an item from the list? How do you pull an item from the filled cart?

Also, (ok two questions), how can you save the built list in the exact order as it was filled?
# Posted By mgfranz | 27/12/11 09:36
dna_84's Gravatar super. Thanks!!! :)
# Posted By dna_84 | 07/04/12 10:56
Back to top