Solved

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

Posted on 2012-03-16
3
1,340 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
  • 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 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