Solved

Reordering items in an ordered list using Jquery, Ajax, PHP & Mysql

Posted on 2012-03-16
3
1,389 Views
Last Modified: 2012-03-20
I'm currently developing a site using PHP/MySQL which is also using Jquery pretty extensively, and I need to add in functionality that allows the user to re-order items within an ordered list using drag and drop.  

When list items are dragged and dropped to a new position within the ordered list -- I'd like to trigger an ajax function that updates the order of those list items in the MySQL database.  My ordered list will have a structure similar to the following (where the dynamic record ID values will be appended each list items ID attributes):

<ul id="task_list_55">
     <li id="task_39"></li>
     <li id="task_40"></li>
     <li id="task_41"></li>
     <li id="task_42"></li>
</ul>

I've achieved the functionality that I've described using Prototype & Scriptaculous in the past -- though I'd much prefer to use JQuery to accomplish this if possible.  How would I accomplish something this using JQuery, PHP, and MySQL ?

One last note -- there will be multiple different ordered lists on the same page -- each with their own unique ID attributes assigned to them -- and each of those ordered lists will need to be sortable.  Please advise on how I would accomplish this.

Thanks!
- Yvan
0
Comment
Question by:egoselfaxis
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
Ahmed Merghani earned 500 total points
ID: 37730190
I suggest to do such thing using jQuery UI. See this link:

http://jqueryui.com/demos/draggable/#sortable

After the ordering, you can handle the changes and trigger the AJax call within these events:
http://jqueryui.com/demos/draggable/#events
0
 

Author Comment

by:egoselfaxis
ID: 37738967
I'm already aware of jquery's ability to do drag n' drop using unordered lists.

I was actually looking for examples of the ajax function triggering (which somehow extracts the record ID values from each list item's ID attribute and passes them to the PHP script that does the re-ordering).  

Are you aware of any tutorials on this (or plugins) that might be helpful?

Thanks,
- yg
0
 

Author Comment

by:egoselfaxis
ID: 37743404
I ended up figuring this out myself:

$(function() {

	$( "ul.tasklist" ).sortable({	
		revert: true,
		stop: function(event, ui) { 
		
			document.getElementById("page").style.opacity = ".15";
			document.getElementById("page").style.filter = "alpha(opacity=15)";
			document.getElementById("loading").style.display = "block";		

			var ul_div_name = "#" + $(this).attr('ID');			
			
			var num_list_items = $(ul_div_name).children().length;
			
			var stage_ul_id = $(this).attr('ID');
			
			var stage_id = stage_ul_id.substring(stage_ul_id.indexOf('_')+6);	
			
			var list_of_task_ids = new Array();
			
			var num = 0;

			$(ul_div_name + ' li').each(function() {
			
				var id = $(this).attr('id').split('_')[1];
				
				list_of_task_ids[num] = id;	

				num++;	
				
			});				
			
			$.ajax({

				type : 'POST',
				url : '../user/packages.php?action=reorderTasks',
				dataType : 'json',
				
				data: {
					stage_id : stage_id,
					num_list_items : num_list_items,
					list_of_task_ids : list_of_task_ids,
				},
				 
				success: function(data) {
				
					document.getElementById("page").style.opacity = "1";
					document.getElementById("page").style.filter = "alpha(opacity=100)";
					document.getElementById("loading").style.display = "none";	
					
					alert(data.msg);		
					
					$('#package_edit').before('<div class="successtext"><input name="dismiss" type="button" class="deletebutton floatright" value="X" onclick="dismissWindow()" /><p><strong>The Tasks have been reordered successfully</strong></p></div>');
					
					$(".successtext").delay(1000).fadeOut(2000);

				},
				 
				error : function(XMLHttpRequest, textStatus, errorThrown) {
				 
					alert('ERROR!');
						
				}
				
			});	
		
		}	
		
	});	
	
});

Open in new window

0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question