jQuery UI Sortable Tutorial

Here's a simple tutorial showing how to use jQuery UI's sortable plugin to update a database table's sort order field on the fly using ajax.
Here's the HTML markup used to define the content that will be sortable (note you don't need to use a ul as most examples will show, you can use any container, and its children will be sortable in this case we used divs.):
<div id="fruit_sort"> <div id="fruit_1">Apple</div> <div id="fruit_2">Orange</div> <div id="fruit_3">Pear</div> </div>
In order to make the fruit_sort container sortable, we need to run $('#fruit_sort').sortable(); at a bare minimum.
But how can we update our database every time the list is sorted? To do this we must bind to the sortable update event. Accordign to the docs this event is triggered when the user stopped sorting and the DOM position has changed.
So here's the JavaScript we are going to use to make our fruit_sort work:
<script language="javascript">
$(document).ready(function(){
$('#fruit_sort').sortable({
update: function(event, ui) {
var fruitOrder = $(this).sortable('toArray').toString();
$.get('update-sort.cfm', {fruitOrder:fruitOrder});
}
});
});
</script>
By calling $(this).sortable('toArray').toString() in our example update event handler we are given a string list of all the item id's, it might look like fruit_2,fruit_1,fruit_3. We can then send the new order to our server in an ajax request using $.get()
Finally to handle things on the server side (using ColdFusion in this demo) we might have some code that looks like this update-sort.cfm:
<cfparam name="url.fruitOrder" type="string"> <!--- fruitOrder = fruit_1,fruit_2, etc...---> <cfset order = 0> <cfloop list="#url.fruitOrder#" index="item"> <cfset fruit_id = Val(ListLast(item, "_"))> <cfset order = order+1> <cfquery> UPDATE fruit SET sort_order = <cfqueryparam value="#order#" cfsqltype="cf_sql_integer"> WHERE fruit_id = <cfqueryparam value="#fruit_id#" cfsqltype="cf_sql_integer"> </cfquery> <cfoutput>#fruit_id#</cfoutput> </cfloop>
Tweet
add to del.icio.us
| Tags: jquery, jqueryui, javascript, js, sortable, ajax
Related Entries
- jQuery UI Autocomple IE 6 Select List z-Index Issues - August 16, 2010
- Using jQuery UI Autocomplete with Hidden ID's - July 14, 2010
- Getting Started with jQuery Mobile - October 29, 2010
- Ext CFC - December 12, 2007
- JavaScript Confirm Modal using Bootstrap - August 21, 2012
Trackbacks
Comments
It uses JSON, so it's a little more difficult than the technique you are using, but it additionally allows for nested relationship data to be exchanged.
<!--- hint: attributes.slideOrder is a list, in the format "slide_{id},...slide_{id}" ---> <cfparam name="attributes.slideOrder">
<cfoutput>
<cfset intPositionBase = 0 /> <cfset strSqlStatement = "" /> <cfset strCurSqlStatement = "" />
<cfloop list="#attributes.slideOrder#" index="strSlideId">
<cfset intPositionBase++ /> <cfset intPosition = intPositionBase * 10> <cfset slideId = ListLast(strSlideId, "_")>
<cfsavecontent variable="strCurSqlStatement"> UPDATE Slides SET intPosition = #intPosition# WHERE id = #slideId#; </cfsavecontent>
<cfset strSqlStatement = strSqlStatement & strCurSqlStatement />
</cfloop>
<cfquery name="rsUpdateSlides" datasource="#request.webDsn#"> #strSqlStatement# </cfquery>
</cfoutput>
Thoughts?
Now I have a string with the list of elements divided by commas on my db.
Whats the best way to display the elements sorted when user loads his page after he sorted, is there a jquery script to resort based on the string I have on my db?
Post a Comment
Recent Entries
- Firefox Aurora now Supports Content Security Policy 1.0
- Writing Secure CFML cfObjective 2013 Slides
- Upgrading to Java 7 on Linux
- J2EE Sessions in CF10 Uses Secure Cookies
- Learn about ColdFusion Security at cfObjective 2013
- Session Loss and Session Fixation in ColdFusion
- FuseGuard 2.3 Released
- CKEditor Spell Checker Plugin





